-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (42 loc) · 1.6 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
"use strict";
let number = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
const displayMessage = function (message) {
document.querySelector(".message").textContent = message;
};
document.querySelector(".check").addEventListener("click", function () {
const guess = Number(document.querySelector(".guess").value);
if (!guess) {
displayMessage("No Number!");
} else if (guess === number) {
displayMessage("Correct Number!");
document.querySelector(".number").textContent = number;
//Changing the style when the answer is correct.
document.querySelector("body").style.backgroundColor = "#60b347";
document.querySelector(".number").style.width = "30rem";
//Checking for the highscore
if (score > highscore) {
highscore = score;
document.querySelector(".highscore").textContent = highscore;
}
} else if (guess !== number) {
if (score > 1) {
displayMessage(guess > number ? "Too High!" : "Too Low!");
score--;
document.querySelector(".score").textContent = score;
} else {
displayMessage("You Lost the Game!");
}
}
});
document.querySelector(".again").addEventListener("click", function () {
score = 20;
number = Math.trunc(Math.random() * 20) + 1;
displayMessage("Start Guessing...");
document.querySelector(".score").textContent = score;
document.querySelector(".number").textContent = "?";
document.querySelector(".guess").value = "";
document.querySelector("body").style.backgroundColor = "#222";
document.querySelector(".number").style.width = "15rem";
});