-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
60 lines (52 loc) · 2.12 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
'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;
}
//Check guess button
document.querySelector('.check').addEventListener('click', function () {
//console.log(document.querySelector('.guess').value);
let guess = Number(document.querySelector('.guess').value);
//first check if there is a value first
if (score > 1) {
if (!guess) {
displayMessage('🛑 no number!');
}
//guess is too high
else if (guess !== secretNumber) {
displayMessage(guess > secretNumber ? '👆 Too High!' : '👇 Too Low!');
document.querySelector('.score').textContent = --score;
}
//guess is correct
else if (guess === secretNumber) {
displayMessage('🎊 Correct Number!');
document.querySelector('.number').textContent = secretNumber;
//acess css with style keyword, and if next word has more then two words use camelCase
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').style.width = '30rem';
if (score > highScore) {
highScore = score;
document.querySelector('.highscore').textContent = highScore;
}
}
}
else {
displayMessage('😭 you lose!');
document.querySelector('.score').textContent = 0;
}
});
//Again button
document.querySelector('.again').addEventListener('click', function () {
score = 20;
secretNumber = Math.trunc(Math.random() * 20) + 1;
//reset score
displayMessage('Start guessing...');
document.querySelector('.score').textContent = score;
document.querySelector('.number').textContent = '?';
document.querySelector('.guess').value = '';
document.querySelector('body').style.backgroundColor = '#222';
//reset size and background color
document.querySelector('.number').style.width = '15rem';
});