Wednesday, August 5, 2009

What is isNaN()?

So earlier I worked through MJWs Avoider Game Tutorial Chapter 11 and got to the challenges. I was excited! I wanted to save some data to the local computer too! So my first addition was going to be adding how many times the player played my game. Sounds easy enough, it was using the same idea as saving the highest score, right?

Well, I figured was as good a place as any -if not better!- to do this. So I added the following lines:

  1. += 1;

  2. trace( ); 

But my output didn't read "1" - it said NaN. Whaaaat?
So I threw in an IF statement:
  1. if( == null )

  2. {

  3. = 0;

  4. }

Haha! MJW helped me to learn to code!
...only problem was: It didn't work. My output still read NaN.
Not one to just ask a question and wait for the answer (sorry, I told you to post a comment on MJW's site if you had a question... I'm a hypocrite) I browsed to Google and searched for an answer.

The answer to my problem was isNaN! Here's what I put in my code:
  1. if( isNaN( )

  2. {

  3. = 0;

  4. }

  5. += 1;

  6. trace( );     

Now that works! I didn't even have to 'import' anything... though I did search for that because I accidentally put isNan (note the lower case 'n') in my code and got an error.

So there you go... isNaN()

No comments:

Post a Comment