-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (53 loc) · 2.11 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
'use strict';
// // DOM manipulation - textContent, value
// console.log(document.querySelector('.message').textContent);
// document.querySelector('.message').textContent = '🎉 Correct Number!';
// console.log(document.querySelector('.message').textContent);
// document.querySelector('.number').textContent = 13;
// document.querySelector('.score').textContent = 10;
// document.querySelector('.guess').value = 19;
// console.log(document.querySelector('.guess').value);
// Event Listener
const giveRandomNumber = function () {
return Math.trunc(Math.random() * 20) + 1;
};
const displayMessage = function (message) {
document.querySelector('.message').textContent = message;
};
let secretNumber = giveRandomNumber();
let score = 20;
let highscore = 0;
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
if (!guess) {
displayMessage('⛔ No number!');
} else if (guess === secretNumber) {
if (highscore < score) {
highscore = score;
}
displayMessage('🎉 Correct Number!');
document.querySelector('.number').textContent = secretNumber;
document.querySelector('.highscore').textContent = highscore;
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
} else if (guess !== secretNumber) {
if (score > 1) {
displayMessage(guess > secretNumber ? '📈 Too high!' : '📉 Too low!');
score--;
document.querySelector('.score').textContent = score;
} else {
displayMessage('💥 You lost the game!');
document.querySelector('.score').textContent = 0;
}
}
});
document.querySelector('.again').addEventListener('click', function () {
secretNumber = giveRandomNumber();
score = 20;
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.score').textContent = score;
displayMessage('Start guessing...');
document.querySelector('.guess').value = '';
document.querySelector('.number').textContent = '?';
document.querySelector('.number').style.width = '15rem';
});