-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
107 lines (99 loc) · 3.4 KB
/
app.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
const inputs = document.querySelector(".inputs"),
resetBtn = document.querySelector(".reset"),
hint = document.querySelector(".hint span"),
wrongLetter = document.querySelector(".wrong span"),
guessLeft = document.querySelector(".guesses span"),
msg = document.querySelector(".msg"),
quitBtn = document.querySelector(".quit"),
body = document.querySelector("body"),
box = document.querySelector(".wrapper"),
typingInput = document.querySelector(".typing-input");
let word, maxGuesses, corrects = [], incorrects = [];
function randomWord() {
let ranObj = wordList[Math.floor(Math.random() * wordList.length)];
word = ranObj.word;
maxGuesses = 8, corrects = [], incorrects = [];
let html = "";
for(let i=0; i<word.length; i++) {
html += `<input type="text" disabled>`;
}
inputs.innerHTML = html;
hint.innerText = ranObj.hint;
guessLeft.innerText = maxGuesses;
wrongLetter.innerText = incorrects;
msg.innerText = "All the Best!"
}
randomWord();
function initGame(event) {
body.style.backgroundColor = "#56baed";
let key = event.target.value;
if(key.match(/^[A-Za-z]+$/) && !incorrects.includes(` ${key.toLowerCase()}`)
&& !corrects.includes(key.toLowerCase())) {
if(word.includes(key)) {
for(let i=0; i<word.length; i++) {
if(word[i] === key) {
inputs.querySelectorAll("input")[i].value = key.toUpperCase();
corrects.push(key.toLowerCase());
}
}
} else {
maxGuesses--;
incorrects.push(` ${key.toLowerCase()}`);
}
guessLeft.innerText = maxGuesses;
wrongLetter.innerText = incorrects;
}
typingInput.value = "";
if(corrects.length === word.length) {
msg.innerText = "Congratulations! You guessed it.";
body.style.backgroundColor = "#6dbb6d";
confetti.start();
setTimeout(function() {
confetti.stop();
}, 1000)
setTimeout(function() {
randomWord();
initGame();
}, 2000);
} else if(maxGuesses < 1) {
msg.innerText = "Game Over!";
for(let i=0; i<word.length; i++) {
inputs.querySelectorAll("input")[i].value = word[i].toUpperCase();
}
body.style.backgroundColor = "#ff4455";
box.classList.add("shake");
setTimeout(function() {
box.classList.remove("shake");
}, 1000)
setTimeout(function() {
randomWord();
initGame();
}, 2000);
}
}
function func() {
typingInput.focus();
}
quitBtn.addEventListener("click", function() {
msg.innerText = "You Quit!";
for(let i=0; i<word.length; i++) {
inputs.querySelectorAll("input")[i].value = word[i].toUpperCase();
}
body.style.backgroundColor = "#ff4455";
box.classList.add("shake");
setTimeout(function() {
box.classList.remove("shake");
}, 1000)
setTimeout(function() {
randomWord();
initGame();
}, 1500);
})
resetBtn.addEventListener("click", randomWord);
resetBtn.addEventListener("click", function() {
maxGuesses = 8;
body.style.backgroundColor = "#56baed";
})
typingInput.addEventListener("input", initGame);
inputs.addEventListener("click", func);
document.addEventListener("keydown", func);