If/ Else if/ Else


#1

var isEven = function(number) {
if(number % 2 === 0) {
return true;
}
else if ( number = isNaN) {
return "isNaN"
}
else {
return false;
}

When i run it says that my function is true for 3.


If/Else if /Else
#2

Your second condition is not checking if number is === to isnan, it's assigning it to isnan.

That is why it skips to else.


If you're having trouble with writing your javascript read this topic to strengthen your code.


#3

Correct but it should not check it either :smile:
It should rather call it:
isNaN(input) is true for if input is not a number and false if it is a number.


#4

Help! I can't figure this out! Can you tell me what is wrong with my code? Thanks a lot!
var isEven = function(number) {
if (number % 2 === 0) {
return ("true");
} else if (isNaN(number) === true) {
return ("Not a number!");
} else {
return ("false");
}
};


#5

You were really close...

First, true and false are booleans, but your making them into strings. Second, return does not need brackets like console.log(), so be sure clear those out as well! See comments below for further clarification:

var isEven = function(number) {
    if (number % 2 === 0) {
        return true;   //Not a string and does not need a bracket!
    } else if (isNaN(number)) {  //Defaults to true, no need to specify
        return "Not a number!";  //Returns do not need brackets!
    } else {
        return false;   //Not a string and does not need a bracket!
    }
};

Hope that helps!


#6

Thanks! It works! It helped a ton!!!


#7

var isEven = function(number) {
if (isNaN(number)) {
return "This not a number";
else if (number % 2 === 0) {
return "it is Even";
}
else {
return "it is odd";
}
}
}
isEven(42);
isEven("word");
isEven('42');

also error


#8

so if number is a string, isNan(number) = false --> else if (condition in bracket is false), woudln't the "Not a number!" not run? because the code under the else if will only run if the condition in the bracket is true?


#9

Yes if and else if only run their cases if the condition is true.
@pingfeng You still use strings as output instead of booleans have a look at @ekfuhrmann 's post.


#10

so this is what i got
var isEven = function(number) {
if (number % 2===0){
return true;
}
else if (isNaN(number)){
return "this is not a number";
}
else {
return false;
}
};
isEven(); //this is where i got stuck a bit as if i put letters in that space gives me an error msg!


#11


yet the code works Ahh the double edged sword!


#12

minus the dollar sign at the end but I'm unsure


#13

What do you mean by this?


#14

I'm unable to enter a string or letter character in the ()


#15

Thats because isEven(); at the end is causing the loop to run, I think you can put a number in but strings wont work


#16

Well if there is an NaN option it makes sense that this is testable, doesn't it?

@wtrinity It really looks good, have you tried refreshing the page?
PS: I tried the exercise with your copyable code and I can pass without error message even if I enter strings.


#17

ah yeah so it does, brain blanked :wink:


#18

thanks for replying so you are stating that if i refresh the page the code i have written will work when i enter a string in the isEven() function
i.e. isEven(hat) ? @haxor789 @benjnev


#19

Well hat is a variable name so at least it has to be "hat" or did you assigned a value to hat?
Yes if you're code is correct and yours seems to be ok then refreshing might help because it resets the javaScript which might help when failed attempts created memory that prevents you from passing.


#20

ok i did not "" the string in the isEven() function and did not declare hat nor assign it
THANK YOU