-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
61 lines (55 loc) · 1.56 KB
/
main.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
let score = document.querySelector(".score");
let timer = document.querySelector(".timer");
let holes = document.querySelectorAll(".hole");
let playBtn = document.querySelector(".play");
let time = 10;
timer.innerHTML = time;
score.innerHTML = 0;
playBtn.addEventListener("click", () => {
playBtn.style.display = "none";
let currHole;
let currScore = 0;
let noOfHoles = Math.floor(Math.random() * 4) + 5;
for (let i = 0; i <= noOfHoles; i++) {
holes[i].style.display = "block";
}
const start = setInterval(() => {
let hole = Math.floor(Math.random() * noOfHoles);
currHole = holes[hole];
let moleImg = document.createElement("img");
moleImg.setAttribute("src", "./mole.png");
moleImg.setAttribute("class", "mole");
currHole.appendChild(moleImg);
let timeout = Math.floor(Math.random() * 300) + 500;
// console.log(timeout);
setTimeout(() => {
currHole.removeChild(moleImg);
}, timeout);
}, 1000);
window.addEventListener("click", (event) => {
if (event.target === currHole) {
currScore += 1;
score.innerHTML = currScore;
}
});
function checkTime() {
if (time === 0) {
playBtn.style.display = "block";
playBtn.innerHTML = "REPLAY";
currScore = 0;
score.innerHTML = 0;
currHole.innerHTML = "";
clearInterval(start);
time = 10;
clearInterval(countdown);
holes.forEach((hole) => {
hole.style.display = "none";
});
}
}
let countdown = setInterval(() => {
time--;
timer.innerHTML = time;
checkTime();
}, 1000);
});