-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (48 loc) · 1.39 KB
/
index.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
let wordBox = document.getElementById("wordBox");
let input = document.getElementById("inputId");
let button = document.getElementById("btn");
let counterBox = document.getElementById("counterBox");
let spanLifePoints = document.getElementById("lifePoints");
let parolaDaIndovinare = ["c", "h", "a", "t", "g", "p", "t"];
let lettereIndovinate = [];
let lifePoints = 6;
function onClick() {
checkLetters();
checkCounter();
}
function checkLetters() {
let valore = input.value.toLowerCase();
if (parolaDaIndovinare.includes(valore)) {
if (!lettereIndovinate.includes(valore)) {
lettereIndovinate.push(valore);
updateWordBox();
}
}
}
function updateWordBox() {
wordBox.textContent = parolaDaIndovinare
.map((lettera) => (lettereIndovinate.includes(lettera) ? lettera : "_"))
.join(" ");
}
function checkCounter() {
if (!parolaDaIndovinare.includes(input.value.toLowerCase())) {
lifePoints--;
spanLifePoints.textContent = lifePoints;
}
if (lifePoints === 0) {
alert("Hai perso! La parola era: ChatGPT! ");
resetGame();
}
if (lettereIndovinate.length === parolaDaIndovinare.length) {
alert("Complimenti! Hai indovinato la parola!");
resetGame();
}
}
function resetGame() {
lettereIndovinate = [];
lifePoints = 6;
spanLifePoints.textContent = lifePoints;
input.value = "";
updateWordBox();
}
button.addEventListener("click", onClick);