kubowania/whac-a-mole

Countdown doesn't work

ShashankAgarwal77 opened this issue · 1 comments

First of all thanks for creating the tutorial on freecodecamp youtube channel

I have been following on the Second Game Project Whack-A-Mole. In the project, your score function works fine but countDown function does not work. I didn't understand what I have done wrong. If someone can help that would be appreciated

Js Code I wrote by the following tutorial

`const square = document.querySelectorAll('.square')
const mole = document.querySelectorAll('.mole')
const timeLeft = document.querySelectorAll('#time-left')
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')

// assign the id of the randomPosition to hitPosition for us to use later
hitPosition = randomPosition.id;

}

square.forEach(id => {
id.addEventListener('mouseup', () => {
if(id.id === hitPosition){
result = result + 1
score.textContent = result
hitPosition = null
}
})
})

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)
}

}

let timerId = setInterval(countDown, 1000)`

Issue solved...using querySelectorAll instead of querySelector in timeLeft object