-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.js
50 lines (47 loc) · 1.61 KB
/
script2.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
'use strict';
const btnAgain = document.querySelector('.again');
const secret = document.querySelector('.number');
const input = document.querySelector('.guess');
const btnCheck = document.querySelector('.check');
const message = document.querySelector('.message');
const scoreEl = document.querySelector('.score');
const highscoreEl = document.querySelector('.highscore');
const body = document.querySelector('body');
let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
btnCheck.addEventListener('click', () => {
const guess = Number(input.value);
if (!guess) {
message.textContent = '⛔️ No Number!!!';
} else if (guess === secretNumber) {
message.textContent = '🎉 Correct Number...';
secret.textContent = secretNumber;
body.style.backgroundColor = '#60b347';
secret.style.width = '30rem';
if (score > highscore) {
highscore = score;
highscoreEl.textContent = highscore;
}
} else if (guess !== secretNumber) {
if (score > 1) {
message.textContent =
guess > secretNumber ? '📈 Too high!' : '📉 Too low!';
score--;
scoreEl.textContent = score;
} else {
message.textContent = '💥 You lost the game!';
scoreEl.textContent = 0;
}
}
});
btnAgain.addEventListener('click', () => {
score = 20;
secretNumber = Math.trunc(Math.random() * 20) + 1;
message.textContent = 'Start Guessing...';
scoreEl.textContent = score;
secret.textContent = '?';
input.value = '';
body.style.backgroundColor = '#222';
secret.style.width = '15rem';
});