-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (48 loc) · 2.11 KB
/
Copy pathscript.js
File metadata and controls
59 lines (48 loc) · 2.11 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
'use strict';
let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
let won = false;
const displayMessage = function (message) {
document.querySelector('.message').textContent = message;
};
document.querySelector('.check').addEventListener('click', function(e) {
let guess = Number(document.querySelector('.guess').value);
console.log(guess, typeof guess);
if (!won && score > 0) {
if (!guess) { // When there is no input
displayMessage('⛔️ No number!');
} else if (guess === secretNumber) { // When the guess is correct
displayMessage('🎉 Correct Number!');
document.querySelector('.number').textContent = secretNumber;
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
won = true;
if (score > highscore) {
document.querySelector('.highscore').textContent = score;
}
document.querySelector('.guess').value = '';
} else if (guess !== secretNumber) { // When the guess is too high
displayMessage(guess > secretNumber ? '🔥 Too high!' : '🔥 Too low!');
score--;
document.querySelector('.score').textContent = score;
}
}
if (!won && score === 0) {
displayMessage('💀 Game over!');
document.querySelector('.number').textContent = secretNumber;
document.querySelector('body').style.backgroundColor = '#e74c3c';
document.querySelector('.number').style.width = '30rem';
}
});
document.querySelector('.again').addEventListener('click', function () {
score = 20;
secretNumber = Math.trunc(Math.random() * 20) + 1;
displayMessage('Start guessing...');
document.querySelector('.score').textContent = score;
document.querySelector('.number').textContent = '?';
document.querySelector('.guess').value = '';
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.number').style.width = '15rem';
won = false;
});