-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
69 lines (59 loc) · 2.19 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
'use strict';
/*console.log(document.querySelector('.message').textContent);
document.querySelector('.number').textContent = 13;
console.log(document.querySelector('.guess').value);
*/
// let's define the secret number
let secretNumber = Math.trunc(Math.random() * 20) + 1;
let score = 20;
let highscore = 0;
const checkFunc = function () {
const guess = Number(document.querySelector('.guess').value);
console.log(guess, typeof guess);
if (!guess) {
// there was no input
document.querySelector('.message').textContent =
'No secretNumber was entered 💀 ';
} else if (guess === secretNumber) {
// player wins
document.querySelector('body').style.backgroundColor = '#60b347';
document.querySelector('.number').textContent = secretNumber;
document.querySelector('.number').style.width = '30rem';
document.querySelector('.message').textContent = 'Correct answer! 🎉🥇🎉';
if (score > highscore) {
highscore = score;
document.querySelector('.highscore').textContent = highscore;
}
}
if (score > 0) {
if (guess > secretNumber) {
// guess too high
document.querySelector('.message').textContent = 'too high📈';
score--;
} else if (guess < secretNumber) {
document.querySelector('.message').textContent = 'too low📉';
score--;
}
document.querySelector('.score').textContent = score;
}
if (score <= 0) {
document.querySelector('.message').textContent = 'You lost ☠☠';
}
};
document.querySelector('.check').addEventListener('click', checkFunc);
///
document.querySelector('.again').addEventListener('click', function () {
score = 20;
document.querySelector('.message').textContent = 'Start guessing...';
document.querySelector('.score').textContent = score;
document.querySelector('.guess').value = '';
document.querySelector('body').style.backgroundColor = '#222';
document.querySelector('.number').style.width = '15rem';
document.querySelector('.number').textContent = '?';
secretNumber = Math.trunc(Math.random() * 20) + 1;
});
document.addEventListener('keydown', function (event) {
if (event.key == 'Enter' && document.querySelector('.guess').value != '') {
checkFunc();
}
});