Home > Enterprise >  i need check for match in different div's
i need check for match in different div's

Time:02-15

hi this is my first project on javascript, I made a memory game with 3 divs in the same section. first div is 8 cards, second 12 and third is 16. and I made this cardCorrect function to count the correct cards, but I need this for the other divs to with different value's! I am now stuck for a long time on this.

I found nothing on google about this

let easyMode = document.getElementById("easy");
let mediumMode = document.getElementById("medium");
let hardMode = document.getElementById("hard");
let controlS = document.getElementById("control");
let back = document.getElementById("menuButtons");

// functions for gamemode buttons and Quit button
function goBack(){
  resetGame();
  back.style.display = "";
  easyMode.style.display = "none";
  mediumMode.style.display = "none";
  hardMode.style.display = "none";
  controlS.style.display= "none";
}

function gameModeEasy() {
  easyMode.style.display = "";
  controlS.style.display= "";
  back.style.display = "none";
}

function gameModeMedium() {
  mediumMode.style.display = "";
  controlS.style.display= "";
  back.style.display = "none";
}

function gameModeHard() {
  hardMode.style.display = "";
  controlS.style.display= "";
  back.style.display = "none";
}
let cardCorrect = 0;

function checkForMatch() {
  let isMatch = firstCard.dataset.image === secondCard.dataset.image;

  isMatch ? disableCards() : unflipCards();
}

function disableCards() {
  firstCard.removeEventListener('click', flipCard);
  secondCard.removeEventListener('click', flipCard);
  cardCorrect  
  console.log(cardCorrect)
  if (cardCorrect === 4) {
    setTimeout(function(){
    alert("Congratulations! You found all the pairs!"); 
  }, 1000)
};

CodePudding user response:

You can set "4" as parameter in disableCards.
Like This,

function disableCards(matchCount) {
  firstCard.removeEventListener('click', flipCard);
  secondCard.removeEventListener('click', flipCard);
  cardCorrect  
  console.log(cardCorrect)
  if (cardCorrect === matchCount) {
    setTimeout(function(){
    alert("Congratulations! You found all the pairs!"); 
  }, 1000)
};

and Call it like this,

function checkForMatch() {
  let isMatch = firstCard.dataset.image === secondCard.dataset.image;
  let matchCount = checkDivAndReturnMatchCount();

  isMatch ? disableCards(matchCount) : unflipCards();
}

(In this example, "checkDivAndReturnMatchCount" function is a new function to check what div you clicked and return correct matchCount value for the div.)

CodePudding user response:

Your Question is not clear for me and i am not able to get you. if you want to access the cards in the second DIV you can your parrentNode() and childNode() functions.

  • Related