Skip to content

Commit a911de4

Browse files
committed
Added Timer to show Mole & Design Preview #day19
1 parent dcedefa commit a911de4

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed
Binary file not shown.

whack-a-mole-game/app.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,34 @@ const grid = document.querySelector(".grid");
22
const mole = document.querySelectorAll(".mole");
33
const score = document.getElementById("score");
44
const timeLeft = document.getElementById("time-left");
5+
const time = document.querySelector(".time");
56

67
let result = 0;
78
let currentTime = timeLeft.textContent; //60
89

10+
// ------ EVENT LISTENERS ------
11+
912
window.addEventListener("DOMContentLoaded", () => {
1013
createGrid();
11-
randomSquare();
14+
let timerId = setInterval(randomSquare, 500);
15+
let countdown = setInterval(() => {
16+
timeLeft.textContent = --currentTime;
17+
if (currentTime <= 0) {
18+
time.textContent="game over!";
19+
grid.removeEventListener("mouseup", whackMole);
20+
clearInterval(countdown);
21+
clearInterval(timerId);
22+
}
23+
}, 1000);
1224
});
1325

26+
grid.addEventListener("mouseup", whackMole = (e) => {
27+
if (e.target.classList.contains("mole")) {
28+
result++;
29+
score.textContent = result;
30+
}
31+
})
32+
1433
// ------ METHODS ------
1534

1635
// method to create grid squares
@@ -33,9 +52,4 @@ randomSquare = () => {
3352
}
3453
let square = squares[Math.floor(Math.random() * 9)]; // adding mole image at random position on the grid
3554
square.classList.add("mole");
36-
square.addEventListener("click", () => {
37-
result++;
38-
score.textContent = result;
39-
40-
})
4155
}

whack-a-mole-game/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<h2>whack a mole</h2>
1313
<h3 style="float: left">your score : <span id="score">0</span></h3>
14-
<h3 style="float: right">time left : <span id="time-left">60</span></h3>
14+
<h3 style="float: right" class="time">time left : <span id="time-left">60</span></h3>
1515
<div class="grid"></div>
1616
</section>
1717
</body>

0 commit comments

Comments
 (0)