If / else if / else , Code not working


#1

My Code should theoretically work, but it always reports "Oops, try again. Make sure your if/else if/else statement returns a string when the function input isn't a number."

var isEven = function(number) {
    if (!(number % 2))
        return true
    else if (isNaN(number))
        return "Syntax error - isEven(): Your input is not a number."
    else
        return false
};

#2

var isEven = function(number) {
    if (number % 2 === 0)
        return true;
    else if (isNaN(number))
        return "Syntax error - isEven(): Your input is not a number.";
    else
        return false;
};

#3

number % 2 === 0

and

!(number % 2)

both evaluate to the same thing, actually


#4

it seems that validation wants number % 2 === 0


#5

Hmm... I guess so. It apparently also worked by reversing the if/else if/

var isEven = function(number) {
    if (isNaN(number))
        return "Syntax error - isEven(): " + number +  " is not a number."
    else if (!(number % 2))
        return true
    else
        return false
};

#6

Does it work? Because problem can be at lines where are return keyword (missed semicolons):

...
return "Syntax error - isEven(): " + number +  " is not a number.";
...
return true;
...
return false;

My code:

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

#7

No, the semicolon is really only needed if you were minimizing it like this:

var isEven=function(n){if(number%2===0){return true;}else if(isNaN(number)){return "Input number is not a number!!";}else{return false;}

but in fact, it's not even needed in those cases either, unless you were to add something after the 'return'(which would be stupid since it would be unreachable), because the bracket signifies the end of the term anyway. Since we only have one command in each, and since it's being programmed vertically, your program can just be

var isEven = function(number) {
    if (number % 2 === 0)
        return true
    else if (isNaN(number))
        return "Input number is not a number!!"
    else
        return false
}
isEven(4)

no semicolons needed, and as I said, since there is only one command in each of the if/else if/else statements, they don't even need brackets


#8

I think it's a bad practice.


#9

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