-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
65 lines (56 loc) · 2.05 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
/** @format */
"use strict";
// Generate Random Number
function randomNumber() {
return Math.trunc(Math.random() * 20) + 1;
}
// Display Message Function
const displayMessage = (message) =>
(document.querySelector(".message").textContent = message);
const body = document.querySelector("body");
const btnCheck = document.querySelector(".check");
const guessText = document.querySelector(".guess");
const secretNumberText = document.querySelector(".number");
const highScoreText = document.querySelector(".highscore");
const scoreText = document.querySelector(".score");
let secretNumber = randomNumber();
let scoreLeft = Number(scoreText.textContent);
let highScore = Number(highScoreText.textContent);
btnCheck.addEventListener("click", function () {
const guess = Number(guessText.value);
if (!guess) {
displayMessage("⚠️ No number!");
} else if (guess === secretNumber) {
displayMessage("👏 You win the game!");
secretNumberText.textContent = secretNumber;
secretNumberText.style.width = "30rem";
body.style.backgroundColor = "#60b347";
if (highScore < scoreLeft) {
highScore = scoreLeft;
highScoreText.textContent = scoreLeft;
}
} else if (guess !== secretNumber) {
scoreLeft--;
scoreText.textContent = scoreLeft;
displayMessage(
guess < secretNumber ? "⚠️ Number too low!" : "⚠️ Number too high!"
);
}
if (scoreLeft === 0) {
displayMessage("Game over!");
btnCheck.style.display = "none";
body.style.backgroundColor = "darkred";
secretNumberText.textContent = secretNumber;
}
});
document.querySelector(".again").addEventListener("click", function () {
scoreLeft = 20;
secretNumber = randomNumber();
displayMessage("Start guessing...");
scoreText.textContent = scoreLeft;
body.style.backgroundColor = "#000";
secretNumberText.textContent = "?";
secretNumberText.style.width = "15rem";
btnCheck.style.display = "block";
guessText.value = "";
});