-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
51 lines (39 loc) · 1.09 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
const square = document.querySelectorAll('.square');
const mole = document.querySelectorAll('.mole');
const timeleft = document.querySelector('#timeleft');
let score = document.querySelector('#score');
let result = 0;
let currentTime = timeleft.textContent;
function randomsquare(){
square.forEach(className =>{
className.classList.remove('mole');
})
let randomPosition = square[Math.floor(Math.random() * 9)];
randomPosition.classList.add('mole');
hitPosition = randomPosition.id
}
square.forEach(id => {
id.addEventListener('mouseup', () =>{
if (id.id === hitPosition) {
result = result+1;
score.textContent = result;
}
})
})
function moveMole(){
let timerid = null;
timerid = setInterval(randomsquare, 1000);
}
moveMole();
function countDown(){
currentTime--;
timeleft.textContent = currentTime;
if (currentTime === 0) {
clearInterval(timerid);
alert('GAME OVER! Your final score is ' + result);
timeleft.textContent = '60';
result = 0;
score.textContent = result;
}
}
let timerid = setInterval(countDown, 1000);