-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (54 loc) · 1.91 KB
/
script.js
File metadata and controls
60 lines (54 loc) · 1.91 KB
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
const containerEL = document.querySelector(".container");
const playAgain = document.querySelector(".play_again");
const hideNum = document.querySelector(".hide_num");
const message = document.querySelector(".message");
const inputNum = document.querySelector(".input_number");
const checkBtn = document.querySelector(".btn_check");
const scoreEL = document.querySelector(".score");
const highScoreEL = document.querySelector(".high_score");
let minvalue = 1;
let maxValue = 20;
let score = 20;
let highScore = 0;
let attempts = 0;
let answer = Math.floor(Math.random() * (maxValue - minvalue + 1));
console.log(answer);
checkBtn.addEventListener("click", () => {
const number = Number(inputNum.value);
if (number) {
if (number != answer) {
attempts++;
if (score > 1) {
score--;
scoreEL.innerHTML = `${score}`;
message.textContent =
number > answer ? "Too High!Try Again...." : "Too Low!Try Again....";
scoreEL.innerHTML = ` ${score}`;
inputNum.value = "";
} else {
message.textContent = `You've lost the game`;
containerEL.style.backgroundColor = "#fff";
scoreEL.textContent = 0;
}
} else {
message.textContent = `Congratulations! You Won it took you ${attempts} attempts :)`;
hideNum.textContent = answer;
hideNum.style.transition = "all 0.5s ease-in";
hideNum.style.width = "50%";
containerEL.style.backgroundColor = "#e0d8d3";
}
} else {
message.textContent = `Please add a number :)`;
}
});
playAgain.addEventListener("click", () => {
score = 20;
scoreEL.innerHTML = `Score: ${score}`;
answer = Math.floor(Math.random() * (maxValue - minvalue + 1));
hideNum.style.width = "25%";
hideNum.textContent = "?";
hideNum.style.transition = "all 0.5s ease-in";
message.textContent = `Start Guessing........`;
inputNum.value = "";
containerEL.style.backgroundColor = "#f4f1ee";
});