-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (61 loc) · 1.92 KB
/
script.js
File metadata and controls
73 lines (61 loc) · 1.92 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
69
70
71
72
73
const scoreDisplay = document.getElementById("score");
const timeDisplay = document.getElementById("time");
const boxes = document.getElementsByClassName("box");
const startBtn = document.getElementById("start-btn");
let score;
let timeLeft;
let activeBox = null;
let isGameRunning = false;
function startGame(){
score = 0;
timeLeft = 30;
scoreDisplay.innerText = score;
timeDisplay.innerText = timeLeft;
startBtn.disabled = true;
isGameRunning = true;
const timeIntervalId = setInterval(function(){
timeLeft--;
timeDisplay.innerText = timeLeft;
if (timeLeft === 0){
clearInterval(timeIntervalId);
};
},1000);
const GameIntervalId = setInterval(function(){
for (let box of boxes){
box.classList.remove("active");
};
activeBox = boxes[Math.floor(Math.random() * boxes.length)];
activeBox.classList.add("active");
setTimeout(function(){
if (activeBox){
activeBox.classList.remove("active");
activeBox = null;
}
},1000);
},2000);
setTimeout(function(){
clearInterval(GameIntervalId);
clearInterval(timeIntervalId);
for(let box of boxes){
box.classList.remove("active");
};
activeBox = null;
alert(`Game Over: Your Final Score is ${score}`);
startBtn.disabled = false;
isGameRunning = false;
},30000);
};
for(let box of boxes){
box.addEventListener("click",function(){
if (!isGameRunning){
return;
};
if (this === activeBox){
score ++ ;
scoreDisplay.innerText = score;
this.classList.remove("active");
activeBox = null;
};
});
};
startBtn.addEventListener("click",startGame);