-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
148 lines (116 loc) · 3.43 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const wordEle = document.getElementById('word');
const wrongLetterEle = document.getElementById('wrong-letters');
const playAgainBtn = document.getElementById('play-button');
const popupContainer = document.getElementById('popup-container');
const notificationContainer = document.getElementById('notification-container');
const finalMessage = document.getElementById('final-message');
const finalMessageReveal = document.getElementById('final-message-reveal-word');
const figurePart = document.querySelectorAll('.figure-part');
// const words = ['application', 'programming', 'interface', 'wizard'];
// let randomWord = words[Math.floor(Math.random() * words.length)];
let randomWord = '';
let playable = true;
let correctLetters = [];
let wrongLetters = [];
//get random word from api
async function getRandomWord() {
const res = await fetch('https://random-word-api.herokuapp.com/word?number=1');
const data = await res.json();
return data[0];
}
// Show hidden word
function showWord() {
wordEle.innerHTML = `
${randomWord
.split('')
.map(
letter => `
<span class="letter">
${correctLetters.includes(letter) ? letter : ''}
</span>
`
)
.join('')}
`;
const innerWord = wordEle.innerText.replace(/[ \n]/g, '');
if (innerWord === randomWord) {
finalMessage.innerText = 'Congratulations! You won! 😃';
finalMessageReveal.innerText = '';
popupContainer.style.display = 'flex';
playable = false;
}
}
// Update the wrong letters
function updateWrongletter() {
// Display wrong letters
wrongLetterEle.innerHTML = `
${wrongLetters.length > 0 ? '<p>Wrong</p>' : ''}
${wrongLetters.map(letter => `<span>${letter}</span>`)}
`;
// Display parts
figurePart.forEach((part, index) => {
const errors = wrongLetters.length;
if (index < errors) {
part.style.display = 'block';
} else {
part.style.display = 'none';
}
});
// Check if lost
if (figurePart.length === wrongLetters.length) {
showGameOver();
}
}
// show game over popup
function showGameOver() {
finalMessage.innerText = 'Unfortunately you lost. 😕';
finalMessageReveal.innerText = `...the word was: ${randomWord}`;
popupContainer.style.display = 'flex';
playable = false;
}
// Show notification
function showNotification() {
notificationContainer.classList.add('show');
setTimeout(() => {
notificationContainer.classList.remove('show');
}, 2000);
}
// Keydown letter press
window.addEventListener('keydown', (e) => {
if (playable) {
if (e.keyCode >= 65 && e.keyCode <= 90) {
const currLetter = e.key.toLowerCase();
if (randomWord.includes(currLetter)) {
if (correctLetters.includes(currLetter)) {
showNotification();
} else {
correctLetters.push(currLetter);
showWord();
}
} else {
if (wrongLetters.includes(currLetter)) {
showNotification();
} else {
wrongLetters.push(currLetter);
updateWrongletter();
}
}
}
}
});
// Restart game and play again
playAgainBtn.addEventListener('click', async () => {
playable = true;
// Empty arrays
correctLetters = [];
wrongLetters = [];
// randomWord = words[Math.floor(Math.random() * words.length)];
randomWord = await getRandomWord();
showWord();
updateWrongletter();
popupContainer.style.display = 'none';
});
(async () => {
randomWord = await getRandomWord();
showWord();
})();