2. If/ Else If/ Else


#1

var isEven = function(number) {
if (number % 2 === 0) {
return true;
}
else if (isNan(number)) {
return "This is not a number";
}
else {
return false;
}
};

what have i done wrong


#2

You forgot to call isEven at the end. After your code add:

var num = prompt("Type a number!");
isEven(num);

#3

it says i have to define isNan, but im not sure where..................


#4

wait nevermind i spelled isNaN wrong XD


#5

lol @blogrunner80368 I should've spotted that one! :wink:
Post again if you need any more help!


#6

lol saw that i was about to type but @cadecodes has got it.


#7

What does it meen "isNan" and when you seen it before while studying(sorry for english, i'm not sure if i wrote wrong :wink: )


#8

nevermind, it's a next exercise


#9

Yes it's the next exercise they are pretty similar and isNaN means "is Not a Number?"


#10

I cannot believe that the "correct" answer uses the modules before testing if dealing with a number.
The asnwer should look something like this, unless someone can explain to me what is wrong with this code:

var isEven = function(number) {

if (isNaN(number){
    return "Invalid Input";
}
else{
    var rem = number % 2;
    if (rem > 0){
        return false;
    }
    else {      
        return true;
    }         
  }

};


#11

var isEven = function(number) {
// Your code goes here!
if(isEven %= 2 ){
return true;
} else if(isNaN(42)){
return true;

} else {
return false;
}
};

it's work :smiley:


#12

The problem is that the SCT probably only looks at the output and as a NaN value fails the modulo operation in the end it somehow "works" but you're absolutely right it's way better to avoid all these ambiguities by just checking if it is a number first.

@3lf
Well it should not:
For example here:

if(isEven %= 2 ){

you're taking the modulo of a function replacing the function with the result.
And here you take isNaN of a constant value:

 else if(isNaN(42)){

meaning this will always produce true no matter what you passed to the function by the parameter number.