-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
70 lines (56 loc) · 1.99 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
63
64
65
66
67
68
69
70
'use strict';
const generateRandomNumber = () => Math.trunc(Math.random() * 20) + 1;
const displayMessage = function (message) {
document.querySelector('.message').textContent = message;
};
const displayNumber = function (number) {
document.querySelector('.number').textContent = number;
};
const displayScore = function (score) {
document.querySelector('.score').textContent = score;
};
const displayHighscore = function (highscore) {
document.querySelector('.highscore').textContent = highscore;
};
let secretNumber = generateRandomNumber();
let score = 20;
let highscore = 0;
document.querySelector('.check').addEventListener('click', function () {
const guess = Number(document.querySelector('.guess').value);
// Where there is no input
if (!guess) {
displayMessage('🛑 No number!');
// When player wins
} else if (guess === secretNumber) {
displayMessage('🎉 Correct Number!');
displayNumber(secretNumber);
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
if (score > highscore) {
highscore = score;
displayHighscore(highscore);
}
// When guess is wrong
} else if (guess !== secretNumber) {
if (score > 1) {
displayMessage(
guess > secretNumber ? '📈 Too high!' : '📉 Too low!'
);
score--;
displayScore(score);
} else {
displayMessage('💥 You lost the game!');
displayScore(0);
}
}
});
document.querySelector('.again').addEventListener('click', function () {
secretNumber = generateRandomNumber();
score = 20;
displayMessage('Start guessing...');
displayNumber('?');
displayScore(score);
document.querySelector('.guess').value = '';
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.number').style.width = '15rem';
});