-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (55 loc) · 2.08 KB
/
app.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
let generatedSecretNumber = 0;
// Generate random secret number
function secretNumber() {
return Math.floor(Math.random() * 10) + 1;
}
// Generate selector title
function selectorInit(selector, phrase) {
let title = document.querySelector(selector);
title.innerHTML = phrase;
}
// Activate buttons and check if user selected option is correct
function start() {
activateButtons();
for (let i = 1; i <= 10; i++) {
document.getElementById(`btn-${i}`).addEventListener('click', () => {
if (i === generatedSecretNumber) {
selectorInit('h6', 'You win!');
disableButtons();
document.querySelector('.start').disabled = true;
document.querySelector('.newGame').disabled = false;
} else if (i > generatedSecretNumber) {
selectorInit('h6', 'Wrong number, try lower!');
document.getElementById(`btn-${i}`).disabled = true;
document.getElementById(`btn-${i}`).classList.add('wrongOption');
document.getElementById(`btn-${i}`).innerText = "x";
}
else {
selectorInit('h6', 'Wrong number, try higher!');
document.getElementById(`btn-${i}`).disabled = true;
document.getElementById(`btn-${i}`).classList.add('wrongOption');
document.getElementById(`btn-${i}`).innerText = "x";
}
});
}
}
// Initialize a new game
function newGame() {
generatedSecretNumber = secretNumber();
selectorInit('h6', 'between 1 and 10');
document.querySelector('.start').disabled = false;
document.querySelector('.newGame').disabled = true;
}
function activateButtons() {
for (let i = 1; i <= 10; i++) {
document.getElementById(`btn-${i}`).disabled = false;
}
}
function disableButtons() {
for (let i = 1; i <= 10; i++) {
document.getElementById(`btn-${i}`).disabled = true;
document.getElementById(`btn-${i}`).classList.remove('wrongOption');
document.getElementById(`btn-${i}`).innerText = i;
}
}
newGame();