2. If / else if / else isNaN help pls


#1



So if I put a number into the function it lets me move on. But when I want to try putting for example the letter j into the function it says j is undefined and gives me an error. So even though it lets me move on if I put a number, it means my code isn't working for when isNaN(number)


var isEven = function(number) {
    if(number % 2 === 0) {
        return true;
    }
    else if(isNaN(number)) {
        return "Not a number, fool!"
    }
    else{
        return false;
    }
};
isEven(j);


#2

if you want to supply a letter as argument, the letter should a string:

isEven("j");

otherwise, j would be a undefined variable


#3

Ok thank you, didn't think to use the quotation. I was curious because I thought anything other than a number would trigger the "else if". I thought the isNaN would trigger with anything other than a number.


#4

if you first define j:

j = "j"
isEven(j);

then it will work. The point is that js will throw an error for an undefined variable.

If i ask you calculate x, you can't do that since you don't have the information needed to calculate x, same in js with undefined variables


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.