-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
193 lines (182 loc) · 3.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const word = document.getElementById("word");
const text = document.getElementById("text");
const scoreElement = document.getElementById("score");
const timeElement = document.getElementById("time");
const endgameElement = document.getElementById("end-game-container");
const settingsButton = document.getElementById("settings-btn");
const settings = document.getElementById("settings");
const settingsForm = document.getElementById("settings-form");
const difficultySelect = document.getElementById("difficulty");
// List of words for game
const words = [
"sigh",
"tense",
"airplane",
"ball",
"pies",
"juice",
"warlike",
"bad",
"north",
"dependent",
"steer",
"silver",
"highfalutin",
"superficial",
"quince",
"eight",
"feeble",
"admit",
"drag",
"loving",
"sigh",
"tense",
"airplane",
"ball",
"pies",
"juice",
"warlike",
"bad",
"north",
"dependent",
"capture",
"stir",
"vanish",
"wise",
"harbor",
"grain",
"vibrant",
"quality",
"restore",
"whisper",
"feast",
"novel",
"mountain",
"intact",
"glow",
"urgent",
"xerox",
"organize",
"unique",
"yacht",
"quilt",
"jump",
"eager",
"jolly",
"zeal",
"quack",
"limb",
"verify",
"upgrade",
"quaint",
"moody",
"naive",
"keen",
"quiver",
"treat",
"glance",
"reward",
"superb",
"swift",
"color",
"voice",
"humor",
"thirst",
"anchor",
"launch",
"smile",
"puzzle",
"script",
"crystal",
"butter",
"figure",
"renew",
"wisdom",
"precise",
"ghost",
"lunar",
"zephyr",
"crisp",
"grace",
"chill",
"violet",
"bubble",
"spark",
"cozy",
"sunrise",
"twist",
"freedom",
"twirl",
"enchanted",
"flame",
"silence",
"whistle",
"echo",
"gentle",
"zenith",
"effort",
"treasure",
"spirit",
"twinkle",
"delight",
"bloom",
];
let randomWord;
let score = 0;
let time = 10;
// let difficulty = "medium";
let difficulty =
localStorage.getItem("difficulty") !== null
? localStorage.getItem("difficulty")
: "medium";
const timeInterval = setInterval(updateTime, 1000);
function getRandomWord() {
return words[Math.floor(Math.random() * words.length)];
}
function addWordToDom() {
randomWord = getRandomWord();
word.innerText = randomWord;
}
function updateScore() {
score++;
scoreElement.innerText = score;
}
function updateTime() {
time--;
timeElement.innerText = time + "s";
if (time === 0) {
clearInterval(timeInterval);
gameOver();
}
}
function gameOver() {
endgameElement.innerHTML = `
<h1>Time ran out</h1>
<p>Your final score is ${score}</p>
<button onclick="history.go(0)">Play Again</button>
`;
endgameElement.style.display = "flex";
}
text.addEventListener("input", (e) => {
const insertedText = e.target.value;
if (insertedText === randomWord) {
e.target.value = "";
addWordToDom();
updateScore();
if (difficulty === "hard") time += 2;
else if (difficulty === "medium") time += 3;
else time += 5;
updateTime();
}
});
settingsButton.addEventListener("click", () =>
settings.classList.toggle("hide")
);
settingsForm.addEventListener("change", (e) => {
difficulty = e.target.value;
localStorage.setItem("difficulty", difficulty);
});
// Init
difficultySelect.value = difficulty;
addWordToDom();
text.focus();