If / else if / else


#1

hi, can you tell me what's wrong with my code? it keeps telling me "Your function returns true for 3!"

here's my code

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

#2

That seems to be an invalid error message.

When you call the function, you are not providing any arguments (isEven();).

Try providing some. That may fix the issue. If not, please let me know.


#3

actually i already tried that whatever i put in isEven(); like isEven(42); is having same error


#4

The problem may lie with your else if statement. While well intentioned, you should probably remove that.

Was that added in by you, or was that part of the original code?

Further if this doesn't solve your problem, can you paste the exercise instructions here?


#5

my instructions

Add an else if branch to your existing if/else statement. If the number put into the function is not a number at all, instead of return true; or return false;, the function should return a string that tells the user that their input isn't a number. (This string can say whatever you like.)


#6

So was the else if the only addition for this lesson?

The else was preexisting?


#7

yes i only need to add else if and others are already added on my prev lesson except for isEven(); also added that for this lesson


#8

How about changing the else if to isNaN(number) instead of writing out the full comparison operator?

edit: just noticed you're checking if isNaN is returning false, which means it is a number.


#9

thanks your correct here's what i did

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

i did that before but with value inside isEven(); and that me makes wrong

:grinning:


#10

Odd.

Well, glad you solved it.

Good work.


#11

same issue not able to enter any string value without error


#12

isEven(horse);

Here, horse is as a variable, not as a string.


#13

mine still doesn't work.

var isEven = function(number) {
if(number % 2 === 0){
return true;
}
else if(isNaN(number)){
return "Input isn't a Number";

     } 
else {
         return "Input isn't a Number";
         }

};
isEven("hello");


#14

Hi. Here's what I did.


#15

i wanted to test the code it should say "this is not a number "
by virtue of what i was asked and that was my aim


#16

ask @haxor789 , stated refreshing the page would allow it to run


#17

look at the code I'm running
first its an if else
second they ask you to incorporate isNaN() function
@amg_nunes


#18

@digitalblaster24420 your problem seems to be that you're returning a string in case of an odd number where the test function expects to see false.


#19

your everywhere!:slightly_smiling:


#20

I'm watching a few JS categories :slightly_smiling: