-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (53 loc) · 1.97 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
58
const refs = {
btnCheck: document.querySelector(".check"),
btnAgain: document.querySelector(".again"),
numberInput: document.querySelector(".number-input"),
randonNumber: document.querySelector(".question"),
guessMessage: document.querySelector(".guess-message"),
score: document.querySelector(".score"),
bodyStyle: document.querySelector("body"),
spanHighScore: document.querySelector(".highscore"),
};
let secretNumber = Math.trunc(Math.random() * 20 + 1); // Отримуємо рандомне число від 1 до 20
let score = 20;
let highScore = 0;
refs.btnCheck.addEventListener("click", function (e) {
e.preventDefault();
const guessingNumber = Number(refs.numberInput.value);
// ----- NO INPUT -----
if (!guessingNumber) {
refs.guessMessage.textContent = "Enter a number!";
// ---- PLEYER WON -----
} else if (guessingNumber === secretNumber) {
refs.guessMessage.textContent = "Great guess!";
refs.randonNumber.textContent = secretNumber;
refs.bodyStyle.style.backgroundColor = "rgb(9, 250, 21)";
refs.randonNumber.style.width = "50rem";
if (score > highScore) {
highScore = score;
refs.spanHighScore.textContent = score;
}
// ----- NUMBER FROM INPUT IS WRONG --------
} else if (guessingNumber !== secretNumber) {
if (score > 1) {
refs.guessMessage.textContent =
guessingNumber > secretNumber ? "High number!" : "Small number!";
score--;
refs.score.textContent = score;
} else {
refs.guessMessage.textContent = "Game Over!";
refs.score.textContent = 0;
}
}
});
// ----- BTN AGAIN -----
refs.btnAgain.addEventListener("click", function () {
secretNumber = Math.trunc(Math.random() * 20 + 1);
score = 20;
refs.guessMessage.textContent = "Start guessing";
refs.randonNumber.textContent = "???";
refs.randonNumber.style.width = "25rem";
refs.score.textContent = score;
refs.numberInput.value = "";
refs.bodyStyle.style.backgroundColor = "rgb(0, 0, 0)";
});