嗨,這是我在 javascript 上的第一個專案,我在同一部分制作了一個帶有 3 個 div 的記憶游戲。第一個 div 是 8 張卡片,第二個 12 和第三個是 16。我制作了這個 cardCorrect 函式來計算正確的卡片,但我需要這個用于其他 div 以具有不同的值!我現在在這個問題上停留了很長時間。
我在谷歌上一無所獲
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)
};
uj5u.com熱心網友回復:
您可以在 disableCards 中將“4”設定為引數。
像這樣,
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)
};
并這樣稱呼它,
function checkForMatch() {
let isMatch = firstCard.dataset.image === secondCard.dataset.image;
let matchCount = checkDivAndReturnMatchCount();
isMatch ? disableCards(matchCount) : unflipCards();
}
(在本例中,“checkDivAndReturnMatchCount”函式是一個新函式,用于檢查您單擊了哪個 div 并為該 div 回傳正確的 matchCount 值。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424733.html
標籤:javascript for循环 比赛 查看
下一篇:使用for回圈將整數添加到陣列中
