forked from abhpd/hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
121 lines (98 loc) · 3.33 KB
/
script.js
File metadata and controls
121 lines (98 loc) · 3.33 KB
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
const wordEl= document.getElementById('word');
const wordLettersEl=document.getElementById('wrong-letters');
const playAgainBtn=document.getElementById("play-again");
const popup=document.getElementById('popup-container')
const notification=document.getElementById('notification-container')
const finalMessage= document.getElementById('final-message');
const figureParts = document.querySelectorAll('.figure-part');
const words=['muggles','wand','alohomora','wizard','witch','lumos','nox','butterbeer','gryffindor', 'ravenclaw', 'hufflepuff','slytherin','mudblood','gillyweeds','hogwarts'];
//to give a random word to check write 'console.log(selectedWord) abd random words
//will show on the console in INSPECT.
let selectedWord= words[Math.floor(Math.random()*words.length)];
const correctLetters=[];
const wrongLetters=[];
//Show hidden words
function displayWord() {
wordEl.innerHTML = `
${selectedWord
.split('')
.map(
letter => `
<span class="letter">
${correctLetters.includes(letter) ? letter : ''}
</span>
`
)
.join('')}
`;
const innerWord= wordEl.innerText.replace(/\n/g,'');
//if the words matches
if(innerWord=== selectedWord)
{
finalMessage.innerText="Merlin's Beard! You did it";
popup.style.display='flex';
}
}
//Update wrong letters
function updateWrongLetterEl(){
//Display Wrong Letters
wordLettersEl.innerHTML=
`${wrongLetters.length >0 ? "<p><b>Wrong</b></p>":''}
${wrongLetters.map(letter => `<span>${letter}</span>`)}
`;
//Display parts of figure
figureParts.forEach((part, index) => {
const errors = wrongLetters.length;
if (index < errors) {
part.style.display = 'block';
} else {
part.style.display = 'none';
}
});
//Check if lost
if(wrongLetters.length===figureParts.length){
finalMessage.innerText="You Lost! Time to go Azkaban!!!"
popup.style.display='flex';
}
}
function showNotification(){
notification.classList.add('show');
setTimeout(()=>{ notification.classList.remove('show');}
,2000);
}
//keydown
window.addEventListener('keydown',e =>
{
//console.log(e.keyCode);
if(e.keyCode>=65 && e.keyCode<=90){
const letter=e.key;
if(selectedWord.includes(letter))
{ //if the letter is correct and not present in the correctLetters array
if(!correctLetters.includes(letter)){
correctLetters.push(letter);
displayWord();
}else{//if the letter is correct but already present a pop up notificatio comes
showNotification();
}
}
else{
if(!wrongLetters.includes(letter)){
wrongLetters.push(letter);
updateWrongLetterEl();
}else{
showNotification();
}
}
}
});
// Restart game and play again
playAgainBtn.addEventListener('click', () => {
// Empty arrays
correctLetters.splice(0);
wrongLetters.splice(0);
selectedWord = words[Math.floor(Math.random() * words.length)];
displayWord();
updateWrongLetterEl();
popup.style.display = 'none';
});
displayWord();