-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (46 loc) · 1.45 KB
/
app.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
const allSquares = document.querySelectorAll('#grid .square')
const mole = document.querySelector('.mole')
const scoreBoard = document.querySelector('#score')
const timer = document.querySelector('#timer')
let randomSquareId
let randomSquare
let score = 0
let time = 60
let selectSquareTimer
scoreBoard.textContent = score
function selectRandomSquare(){
allSquares.forEach(square =>{
return square.classList.remove('mole')
})
randomSquareId = Math.floor(Math.random() * 9);
const randomSquare = allSquares[randomSquareId]
randomSquare.classList.add('mole')
randomSquare.addEventListener('mousedown',hitMole)
}
function moveMole(){
selectSquareTimer = setInterval(selectRandomSquare,500)
}
function hitMole(){
const hitPosition = this.getAttribute('id')
if(parseInt(hitPosition) === (randomSquareId+1)){
score++;
}
scoreBoard.textContent = score
}
function setTimer(){
time--;
timer.innerHTML=time
if(time === 0){-
clearInterval(countDownTimer)
clearInterval(selectSquareTimer)
if(confirm(`GAME OVER! You Score is ${score}\nReload the game?`)){
location.reload()
}else{
score = 0
scoreBoard.textContent=score
allSquares[randomSquareId].classList.remove('mole')
}
}
}
let countDownTimer = setInterval(setTimer, 1000)
moveMole()