Exercise 2 - if else and using isNaN


#1

Hi all,
I'm not clear how to use the isNaN function to write the expression within my code. I've tried different variations but my syntax is unsuccessful.

Any helpful explanations would be appreciated. My code is below.

var isEven = function(number) {
// Your code goes here!
if( number % 2 ==0){
return true;
}
else if(isNaN (number));{
return "it's not a number";
}
else{
return false;
}
};


#2

@sunnyjeans,

You are using a semi-colon-; at the end of your IF-condition
else if(isNaN (number));
For the Javascript-Interpreter the semi-colon-; is seen as an End-of-Statement indicator
causing

  • the IF-statement is ended, and the code-block { return "it's not a number"; } is NOT run
  • the Javascript Interpreter is NOT expecting an ELSE-statement and throws an Error

Remove the semi-colon-;

http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements
[ quote]
The point is that even if-else must end with ; if it is following by a single statement, like
if (condition) statement;

The reason that if-else does NOT end with a semi-colon-;
when express {group of statement }
because a semi-colon-;
is NOT a symbol for ending {group of statement },
but IS a symbol to end a single statement.
[end-quote]

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/block


#3

Thanks for your help Leonhard, I can see where I went wrong. :slightly_smiling: