I've been researching how to fix my issue for the past week but can't. I am creating a "click a card and it adds to score" card game but I am having trouble getting the clicks to stop after x amount of cards have already been clicked. I have created a clickCounter function that tracks the clicks and executes with every click. I have also added an isGameOver boolean to stop the function once it hits a certain amount of clicks. However, the boolean doesn't execute as I'd hoped in the clickCounter function. Long story short: I just want the game to end once x amount of cards have been clicked.
Here is the JavaScript:
let isGameOver = false;
let counter = 0;
const clickCounter = function () {
switch (counter) {
case 0:
case 1:
counter += 1
break;
case 2:
isGameOver; //this is where I want the game to end//
break;
}
//this keeps track of the counts in console//
console.log(`counter equals: ${counter}`);
}
//this is my function that runs everything//
const cardSelection = function () {
randomNumber() //just a function to generate random #//
//beginning of if statement//
if(!isGameOver) {
//CARD ONE//
document.querySelector('#cardOne').addEventListener('click', function (e) {
cardOneFront(); //this changes card appearance, ignore please//
clickCounter(); //clickCounter is being called here//
if (randomNums[0]) {
scoreDisplay.innerText = `SCORE: ${scoreCount += randomNums[0]}`;
console.log(`score just added: ${randomNums[0]}`);
}
this.style.pointerEvents = 'none'
});
//CARD TWO//
document.querySelector('#cardTwo').addEventListener('click', function () {
cardTwoFront(); //this changes card appearance, ignore please//
cardCounter(); //clickCounter is being called here//
if (randomNums[1]) {
scoreDisplay.innerText = `SCORE: ${scoreCount += randomNums[1]}`;
console.log(`score just added: ${randomNums[1]}`);
}
this.style.pointerEvents = 'none'
});
} //if statement ends here//
} //cardSelection function ends here//
cardSelection()
I have visualization of it too. See Here: what appears in console
I have 10+ cards but I only included two here to keep the code from being super long but the cards all have the similar code and their own individual click events. So, according to the image above, I want card three to not be clickable after counter = 2. But, as you see in the image, even though counter has equaled 2, card 3 is clickable anyways.
I hope this explanation was thorough and I appreciate any help as I am newer to coding. Also, I am coding all of this using vanilla JavaScript.
Thank you!