-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
54 lines (49 loc) · 1.77 KB
/
logic.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
let cnum = Math.floor(Math.random() * 100) + 1;
let input = document.getElementById("inp");
let reset = document.getElementById("reset");
let message = document.getElementById("Message");
let subBtn = document.getElementById("submit");
let attempt = document.getElementById("Attempts");
let att = 0;
let confettiCanvas = document.getElementById("confetti");
let confettiSettings = { target: confettiCanvas };
let confetti = new ConfettiGenerator(confettiSettings);
document.getElementById("start").addEventListener("click", function() {
document.getElementById("welcome").style.display = "none";
document.getElementById("game").style.display = "block";
});
function check() {
let usernum = parseInt(input.value);
if (cnum === usernum) {
message.innerHTML = "Congratulations, You got the number!";
message.style.color = "green";
reset.style.display = "block";
subBtn.disabled = true;
confettiCanvas.style.display = "block";
confetti.render();
setTimeout(function() {
confetti.clear();
confettiCanvas.style.display = "none";
}, 5000);
} else if (cnum < usernum) {
message.innerHTML = "HINT: Number is lower than your guess";
message.style.color = "red";
} else {
message.innerHTML = "HINT: Number is higher than your guess";
message.style.color = "red";
}
att++;
attempt.innerHTML = att;
input.value = "";
}
subBtn.addEventListener("click", check);
reset.addEventListener("click", function() {
cnum = Math.floor(Math.random() * 100) + 1;
input.value = "";
att = 0;
attempt.innerHTML = att;
message.innerHTML = "";
message.style.color = "black";
reset.style.display = "none";
subBtn.disabled = false;
});