Monday 15 July 2013

javascript - Why is this if statement always true when it shouldn't be? -



javascript - Why is this if statement always true when it shouldn't be? -

i'm making game goal pairs of 4 using normal card deck. when prompt card value player wants inquire player, else if (playerarray[turn].cards.indexof(card) == -1) evaluates true, when card variable contains value of cards array. makes no sense me @ , i'm stuck on how prepare this. suggestions appreciated.

var playerarray = []; //holds player objects var turn = 0; //keeps track of turn var wrongcard = true; var card = askcard(player); //prompt 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 + ', your:'); homecoming whichcard; } while (wrongcard) { //run long card input wrong if (card < 1 || card > 13) { //if card value not between 1-13 card = askcard(player); } //if asked card doesn't exist in 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 + ', your:'); } else { wrongcard = false; //jump out of loop } }

use

playerarray[turn].cards.indexof(parseint(card,10)) == -1)

javascript

No comments:

Post a Comment