-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (59 loc) · 1.79 KB
/
script.js
File metadata and controls
68 lines (59 loc) · 1.79 KB
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
'use strict';
// GLOBAL VARIABLES
let score = 20;
let highscore = 0;
let state = true;
// DOM ELEMENTS
const body = document.querySelector('body');
const again = document.querySelector('.again');
const number = document.querySelector('.number');
const guess = document.querySelector('.guess');
const check = document.querySelector('.check');
const message = document.querySelector('.message');
const label_score = document.querySelector('.score');
const label_highscore = document.querySelector('.highscore');
// DOM DELEGATION
let randomNumber = Math.trunc(Math.random() * 20) + 1;
function gameLogic() {
if (state) {
if (!guess.value) {
return (message.textContent = 'Please enter a number!');
}
const checkValue = Number(guess.value);
if (checkValue === randomNumber) {
message.textContent = 'Correct Number';
body.style.backgroundColor = '#60b347';
label_score.textContent = score;
number.textContent = randomNumber;
state = false;
if (score > highscore) {
highscore = score;
label_highscore.textContent = highscore;
}
} else if (checkValue > randomNumber) {
message.textContent = 'Too High!';
score = score - 1;
} else {
message.textContent = 'Too Low';
score = score - 1;
}
} else {
message.textContent = 'Please restart again!';
body.style.backgroundColor = '#222';
}
}
check.addEventListener('click', () => {
gameLogic();
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter') gameLogic();
});
again.addEventListener('click', () => {
score = 20;
state = true;
message.textContent = 'Start guessing...';
label_score.textContent = score;
body.style.backgroundColor = '#222';
randomNumber = Math.trunc(Math.random() * 20) + 1;
number.textContent = '?';
});