-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (53 loc) · 1.74 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
60
61
62
'use strict';
const checkBtn = document.querySelector('.check');
const guessInput = document.querySelector('.guess');
const again = document.querySelector('.again');
const numberEl = document.querySelector('.number');
const scoreEl = document.querySelector('.score');
const highscoreEl = document.querySelector('.highscore');
const messageEl = document.querySelector('.message');
const body = document.querySelector('body');
let secretNumber = Math.floor(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
const displayMessage = function (message) {
messageEl.textContent = message;
};
checkBtn.addEventListener('click', function () {
const guess = Number(guessInput.value);
console.log(guess, typeof guess);
// When there is no input
if (!guess) {
displayMessage('⛔ No number!');
// When player wins
} else if (guess === secretNumber) {
displayMessage('🎉 Correct Number!');
numberEl.textContent = secretNumber;
body.style.backgroundColor = '#60b347';
numberEl.style.width = '30rem';
if (score > highscore) {
highscore = score;
highscoreEl.textContent = highscore;
}
// When guess is wrong
} else if (guess !== secretNumber) {
if (score > 1) {
displayMessage(guess > secretNumber ? '📈 Too high!' : '📉 Too low!');
score--;
scoreEl.textContent = score;
} else {
displayMessage('💥 You lost the game!');
scoreEl.textContent = 0;
}
}
});
again.addEventListener('click', function () {
score = 20;
secretNumber = Math.floor(Math.random() * 20) + 1;
displayMessage('Start guessing...');
scoreEl.textContent = score;
numberEl.textContent = '?';
guessInput.value = '';
body.style.backgroundColor = '#222';
numberEl.style.width = '15rem';
});