-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (50 loc) · 1.77 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
'use strict';
let secretNumb = Math.floor(Math.random() * 20 + 1);
const displayMessage = function (message) {
document.querySelector('.message').textContent = message;
};
let score = 20;
let highscore = 0;
// Check button
document.querySelector('.check').addEventListener('click', function () {
let guess;
guess = Number(document.querySelector('.guess').value);
// when input is empty
if (!guess) {
displayMessage('no number !!!');
}
// when input matches
else if (guess === secretNumb) {
displayMessage('Correct Number ');
document.querySelector('.number').textContent = secretNumb;
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
if (score > highscore) {
highscore = score;
}
document.querySelector('.highscore').textContent = highscore;
}
// when input does not match
else if (guess !== secretNumb) {
if (score > 1) {
displayMessage(guess > secretNumb ? 'too high :|' : 'too low :|');
score--;
document.querySelector('.score').textContent = score;
} else {
displayMessage('You lost the game :(');
document.querySelector('.score').textContent = 0;
document.querySelector('body').style.backgroundColor = 'red';
}
}
// Again button
document.querySelector('.again').addEventListener('click', function () {
score = 20;
document.querySelector('.score').textContent = score;
document.querySelector('.guess').value = '';
secretNumb = Math.floor(Math.random() * 20 + 1);
displayMessage('start guessing...');
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.number').style.width = '15rem';
document.querySelector('.number').textContent = '?';
});
});