javascript – 警报消息错误

我目前刚接触
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!");
}
点赞