Does anybody know what I'm doing wrong? please and thank you! :)


#1

I'm stuck on 9.More on Control Flow in Java Script.
This is my code:
var isEven = function(number) {
// Your code goes here!
if( number %2 === 0)
{
return true;
}
else if ( number %2 != 0)
{
return false;
}
else if (isNaN (number))
{
return number + " " + "is a number";
}
else
{
return "number is a string";
}
};

This is the error message that I'm getting: "Oops, try again. Make sure your if/else if/else statement returns a string when the function input isn't a number."


#2

Your

else if (isNaN (number)) {
    return number + " " + "is a number";
}

needs to be before your

else if ( number %2 != 0) {
    return false;
}

It is because your function produces false on a NaN type as you have number % 2 != 0 before checking whether the input is a number.


#3

Well, change logic a bit. Start from "is it a number at all?"

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

This works


#4

Thank you so much! that worked!


#5

Thank you so much! :slight_smile:


#6

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