-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (71 loc) · 2.45 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// selecting elements
const mainContainer = document.querySelector(".main-container");
const btnAgain = document.querySelector(".btn-again");
const btnCheck = document.querySelector(".btn-check");
const secretNumberText = document.querySelector(".secret-number");
const guessingTxt = document.querySelector(".guessing-txt");
const scoreTxt = document.querySelector(".value-score");
const highScoreTxt = document.querySelector(".value-high-score");
let secretNumber;
let score;
let highScore = 0;
let isPlaying;
// init values
score = 20;
isPlaying = true;
scoreTxt.textContent = score;
secretNumber = Math.trunc(Math.random() * 20) + 1;
// init function
const initValues = function () {
score = 20;
isPlaying = true;
scoreTxt.textContent = score;
secretNumber = Math.trunc(Math.random() * 20) + 1;
document.querySelector(".guess-input").value = 0;
mainContainer.classList.remove("loser-class");
mainContainer.classList.remove("winner-class");
secretNumberText.textContent = "?";
guessingTxt.textContent = "Start guessing";
};
// check input against secret number
btnCheck.addEventListener("click", function () {
const guessInput = Number(document.querySelector(".guess-input").value);
if (isPlaying) {
if (score > 1) {
if (guessInput === secretNumber) {
guessingTxt.textContent = "Correct!";
secretNumberText.textContent = secretNumber;
mainContainer.classList.add("winner-class");
isPlaying = false;
if (score > highScore) {
highScore = score;
highScoreTxt.textContent = highScore;
}
} else if (guessInput > secretNumber) {
score--;
scoreTxt.textContent = score;
guessingTxt.textContent = "Too high!";
} else if (guessInput < secretNumber) {
score--;
scoreTxt.textContent = score;
guessingTxt.textContent = "Too low!";
}
} else {
scoreTxt.textContent = 0;
guessingTxt.textContent = "You lost the game!";
secretNumberText.textContent = secretNumber;
mainContainer.classList.add("loser-class");
}
}
});
btnAgain.addEventListener("click", function () {
document.querySelector(".guess-input").value = 0;
secretNumber = Math.trunc(Math.random() * 21);
score = 20;
scoreTxt.textContent = score;
isPlaying = true;
mainContainer.classList.remove("loser-class");
mainContainer.classList.remove("winner-class");
secretNumberText.textContent = "?";
guessingTxt.textContent = "Start guessing";
});