PDA

View Full Version : using NaN in an if statement



mojoNYC
February 16th, 2003, 02:19 PM
ive got an array of cards 2-10 plus jack, queen, king, ace and i'm pulling members from the array at random--in order to score it, i decided to check the first character of the string and see if it returns a number or not.
if it's a number, add the number to the score, but if it returns 'NaN' (not a number) then it will just add 10 to the score--so even tho NaN is defined as a variable in flash, somehow my if statement 'char==NaN' isn't working...


functionÊscoreCard(){
//get first member of string, check to see if its a number
ÊÊÊÊchar=randMember.charAt(0);
ÊÊÊchar=Number(char);
ÊÊÊÊif(char==NaN){
ÊÊÊÊÊÊÊÊtrace("Hi");
ÊÊÊÊÊÊÊÊscore+=10;
ÊÊÊÊÊÊÊÊ}elseÊ//trace(char);
ÊÊÊÊÊÊÊtrace("Lo");
ÊÊÊÊÊÊÊÊscore+=char;
ÊÊÊÊ//trace(score);
}

does somebody have the goods on how to do this right?
i've attached a fla of it in the thread titled 'arrays??!!'
thanks,
-mojo

mojoNYC
February 16th, 2003, 02:22 PM
i should also say that it works right to add the 2-9 numbers to the score, and that it does return 'NaN' if its a high card, but it doesn't evaluate true in the line 'if(char==NaN)'

lostinbeta
February 16th, 2003, 02:35 PM
Try

if (isNaN(char)) {
trace("Hi");
score += 10;
} else {
trace("Lo");
score += char;
}

mojoNYC
February 16th, 2003, 03:00 PM
right on!
thanks for the tip(-:
-mojo

lostinbeta
February 16th, 2003, 03:02 PM
No problem :)