-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (55 loc) · 1.78 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
71
72
73
74
'use strict';
let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
const displayMessage = function(message) {
document.querySelector('.message').textContent = message;
}
const showHighscore = () => {
document.querySelector('.highscore').textContent = highscore;
}
const showScore = () => {
document.querySelector('.score').textContent = score;
}
const numberSelector = document.querySelector('.number');
const bodySelector = document.querySelector('body');
const guessSelector = document.querySelector('.guess');
document.querySelector('.check').addEventListener('click',function() {
const guess = Number(document.querySelector('.guess').value);
// When there is no input
if (!guess) {
displayMessage("⚠️ No Number!⚠️");
// When player wins
} else if (guess === secretNumber) {
displayMessage('🎯 Correct Number!')
numberSelector.textContent = secretNumber;
bodySelector.style.backgroundColor = '#60b347'
numberSelector.style.width = '30rem'
if(score > highscore) {
highscore = score
showHighscore();
}
// When guess is wrong;
} else if (guess !== secretNumber) {
if(score > 1) {
displayMessage(guess > secretNumber ? '⏫ Too high!!' : '⏬ Too low!!')
score = score -1;
showScore();
} else {
score = 0;
showScore()
displayMessage('😢 You lost the game!')
}
}
});
// When playing again.
document.querySelector('.again').addEventListener('click', function() {
score = 20;
secretNumber = Math.trunc(Math.random() * 20) + 1;
displayMessage('Start guessing...');
showScore();
numberSelector.textContent = '?';
guessSelector.value = '';
bodySelector.style.backgroundColor = '#222'
numberSelector.style.width = '15rem'
});