gpt4 book ai didi

javascript - 为什么这个 if 语句在不应该为真时却总是为真?

转载 作者:行者123 更新时间:2023-11-28 19:32:36 25 4
gpt4 key购买 nike

我正在制作一款游戏,目标是使用普通的牌组获得 4 对。当我提示玩家想要询问另一位玩家哪个卡牌值时,else if (playerArray[turn].cards.indexOf(card) == -1) 始终评估为 true,即使卡变量包含卡数组的值。这对我来说根本没有意义,我一直在思考如何解决这个问题。非常感谢任何建议。

var playerArray = [];       //Holds the player objects
var turn = 0; //Keeps track of whose turn it is
var wrongCard = true;
var card = askCard(player); //Prompt to get a card value

function askCard(player) {

var whichCard = prompt('The deck has: ' + deck.length + ' cards left in it.\n'
+ 'Your current cards are: ' + playerArray[turn].cards.join(' - ') + '\n'
+ player + ', all your:');

return whichCard;
}

while (wrongCard) { //Run as long as the card input is wrong

if (card < 1 || card > 13) { //If card value is not between 1-13
card = askCard(player);
}

//If the asked card doesn't exist in the current player's cards array
else if (playerArray[turn].cards.indexOf(card) == -1) { //ALWAYS TRUE, WHY?
card = prompt('The deck has: ' + deck.length + ' cards left in it.\n' +
'Your current cards are: ' + playerArray[turn].cards.join(' - ')
+ '\n' + player + ', all your:');
}

else {
wrongCard = false; //Jump out of loop
}
}

最佳答案

使用

playerArray[turn].cards.indexOf(parseInt(card,10)) == -1)

关于javascript - 为什么这个 if 语句在不应该为真时却总是为真?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26472174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com