-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (64 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
75
76
77
78
79
let startBtn = document.getElementById('start-button');
let round = 0;
let colors = ['green', 'red', 'yellow', 'blue'];
let btns = Array.from(document.querySelectorAll('.color-button'));
let userBlinkClick = [];
let systemBlink = [];
startBtn.addEventListener('click', Start);
function Start() {
resetGame();
levelup();
}
function userInput(event) {
const clickedBtn = event.target;
userBlinkClick.push(clickedBtn);
checkUserInput();
}
function randBlink() {
let randNumber = Math.floor(Math.random() * 4);
const btnToBlink = btns[randNumber];
btnToBlink.classList.add('blink');
setTimeout(() => {
btnToBlink.classList.remove('blink');
}, 500);
systemBlink.push(btnToBlink);
enableUserInput();
}
function levelup() {
document.getElementById('start-button').innerText = "Restart";
document.getElementById('message').innerHTML = "Restart The game.";
round++;
document.getElementsByClassName('level')[0].innerText = `Round: ${round}`;
randBlink();
}
function enableUserInput() {
btns.forEach(btn => {
btn.addEventListener('click', userInput);
});
}
function disableUserInput() {
btns.forEach(btn => {
btn.removeEventListener('click', userInput);
});
}
function checkUserInput() {
let currentClickIndex = userBlinkClick.length - 1;
if (userBlinkClick[currentClickIndex] !== systemBlink[currentClickIndex]) {
alert(`Game Over! You clicked the wrong button. Score : ${round}`);
resetGame();
return;
}
if (userBlinkClick.length === systemBlink.length) {
disableUserInput();
setTimeout(() => {
userBlinkClick = [];
levelup();
}, 1000);
}
}
function resetGame() {
round = 0;
systemBlink = [];
userBlinkClick = [];
document.getElementsByClassName('level')[0].innerText = `Round: 0`;
}