【发布时间】:2022-10-01 08:05:24
【问题描述】:
当我的卡片记忆游戏中的所有卡片都匹配时,有人可以帮我添加“获胜”警报吗?
我是编码新手。我只是对功能没有完全了解。
当我点击一张卡片时,我可以得到一个弹出的提醒,但不是在它们全部匹配之后。
这是我的 JS:
const cards = document.querySelectorAll(\'.memory-card\');
/*Create two variables using let: hasFlippedCard and lockBoard. Both should be false*/
let hasFlippedCard = false;
let lockBoard = false;
let firstCard, secondCard;
function flipCard() {
if (lockBoard) return;
if (this === firstCard) return;
this.classList.add(\'flip\');
if (!hasFlippedCard) {
// first click
hasFlippedCard = true;
firstCard = this;
return;
}
// second click
secondCard = this;
checkForMatch();
}
function checkForMatch() {
let isMatch = firstCard.dataset.framework === secondCard.dataset.framework;
isMatch ? disableCards() : unflipCards();
}
function disableCards() {
firstCard.removeEventListener(\'click\', flipCard);
secondCard.removeEventListener(\'click\', flipCard);
resetBoard();
}
function unflipCards() {
lockBoard = true;
setTimeout(() => {
/*Replace the # symbols with the appropriate class*/
firstCard.classList.remove(\'flip\');
secondCard.classList.remove(\'flip\');
resetBoard();
}, 1500);
}
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
(function shuffle() {
cards.forEach(card => {
/*Replace # with the appropriate numeric value for your game*/
let randomPos = Math.floor(Math.random() * 12);
card.style.order = randomPos;
});
})();
/*Replace # with the appropriate event listener*/
cards.forEach(card => card.addEventListener(\'click\', flipCard));
-
将您的 html 添加到 sn-p 以便我们可以使用
-
您缺少卡的总数和已找到所有卡的检查。
标签: javascript