So basically, all I’m trying to do is to stop the button click that runs the entire dice game from being able to be clicked once the condition of “gameWon” is met
so, I wrapped the entire function with an if “gameWon = false” then the button can be clicked.
and at the bottom I made it to where if a score reaches 20 points, then gameWon = true.
so, you win the game once your score reaches 20. and once it reaches 20 it sets the gameWon to true. So why am I still able to click the button infinately?
I’m sure it’s something super simple but I can’t get this button function to quit working once a condition is met.
I’ve also console logged it and gameWon does indeed switch to true once a score goes above 20. So, what is happening here?
let playerScoreDisplayOne = document.getElementById('player-score-display-one')
let playerScoreDisplayTwo = document.getElementById('player-score-display-two')
let playerDiceDisplayOne = document.getElementById('player-dice-display-one')
let playerDiceDisplayTwo = document.getElementById('player-dice-display-two')
let playerTurnHeader = document.getElementById('player-turn-header')
let diceRollButton = document.getElementById('dice-roll-button')
let playerOneTurn = true
let gameWon = false
let player1score = 0
let player2score = 0
if (gameWon === false) {
diceRollButton.addEventListener('click', function example() {
randomNumber = Math.floor(Math.random() *6 ) + 1
if (playerOneTurn === true) {
playerDiceDisplayOne.textContent = randomNumber
playerScoreDisplayOne.textContent = player1score += randomNumber
playerTurnHeader.textContent = 'Player 2 turn!'
playerDiceDisplayOne.classList.remove('active')
playerDiceDisplayTwo.classList.add('active')
playerOneTurn = false
} else if (playerOneTurn === false) {
playerDiceDisplayTwo.textContent = randomNumber
playerScoreDisplayTwo.textContent = player2score += randomNumber
playerTurnHeader.textContent = 'Player 1 turn!'
playerDiceDisplayOne.classList.add('active')
playerDiceDisplayTwo.classList.remove('active')
playerOneTurn = true
}
if (player1score >= 20) {
playerTurnHeader.textContent = 'Player 1 wins!!!'
gameWon = true
} else if (player2score >= 20) {
playerTurnHeader.textContent = 'Player 2 wins!!!'
gameWon = true
}
console.log(gameWon)
})
}