我目前不熟悉JavaScript,正在学校学习!我正在通过创建游戏来完成一项任务,但是警报不断弹出并提示错误的游戏。每次警告为“您找到一个匹配项!”对于每张卡,但这都不应该发生。在过去的一个小时里,我一直在努力解决这个问题。谢谢

var cards = ["queen", "king", "queen", "king"];

var cardsInPlay = [];

var cardOne = cards[0];

cardsInPlay.push(cardOne);

console.log("User flipped " + cardOne);

var cardTwo = cards[1];

cardsInPlay.push(cardTwo);

console.log("User flipped " + cardTwo);

if (cardsInPlay.length === 2){
    cardsInPlay[0] === cardsInPlay[1];
    alert("You found a match!");
} else {
        alert("Sorry, try again");
    }

最佳答案

我认为您希望提出如下条件:

if (cardsInPlay.length === 2 && cardsInPlay[0] === cardsInPlay[1]) {
  alert("You found a match!");
}

09-11 07:37