If / else if / else


#1

ErrorOops, try again. Your function returns true for 3!

var isEven = function(number) {
  // Your code goes here!
  if(number%2===0)
  {
      return "true Value";
  }
 
  else if(isNaN("berry")===true)
  {
      return "Entered String isn't a Number";
  }
    else
  {
    return "false Value";
  }
};

isEven("vipul");

Help me in this thing.

Wheather i return a string or boolean (true\false) error remain the same.
Kindly help me out where i am loosing in concept.

Thank You


#2

var isEven = function(number) {
    if (number%2===0) {
        return true; //this should be a boolean
    } else if(isNaN(number)===true) { // I don't understand what berry was.
        return "Entered String isn't a Number";
    } else {
        return false; //this should be a boolean
    }
};

#4

I have a question on this one. Why isn't isNaN(number)=true acceptable? I thought the isNaN func yielded boolean values?


#5

isNaN(number) = true is improper syntax, you are trying to assign the (not a) variable isNaN(number) to true. isNaN(number) == true, however, would work, as == is a less strict form of ===.


#6

Thanks. I wasn't thinking of it as an assignment. Just got used to true and false as boolean values.


#7

I don't know what was the issue, earlier. But today i made the return statement as boolean and now it worked. Thank You. And applozie for berry thing :smile:


#8

Because you are doing it wrong mate


#9

Try with the above changes


#10

but @siddy335 if number = 3 then this will return false vaue


#11

Thanks. Someone else gave me a tip in a different thread.


#12

Yes if the number is 3 then it will return the false value as the conditon is if(number%2===0) which means a number divisible by 2 leaving 0 as the remainder. If and only if the remainder is 0 then only the condition is true. Now if you divide 3/2 it will not leave the remainder as 0, hence the condition is false.


#13

my code is below , I am stuck

var isEven = function(number) {
if ( number%2 === 0)
{
return "true value";
}
else if (isNaN(number))
{
return "Entered string is not a number";
}
else
{
return "false value";
}

};

when submit it , it appear

Oops, try again. Looks like your function returns false when number = 3. Check whether your code inside the if/else statement correctly returns true if the number it receives is odd.

what I do ?


#14

Your code should return true if the number it receives is odd, not even.


#15

what is the solution of my above problem


#16

return true if number is odd, (number % 2 == 1).


#17

Doesn't the exercise call for returning 'true' if the number is evenly divisible by 2? The "number % 2 === 0" should "return true".

"Write an if / else statement inside the isEven function. It should "return true;" if the number it receives is evenly divisible by 2. Otherwise (else), it should "return false;"."


#18

I think @arccoder77240 is on the next one, because the error says


#19

To anyone reading the above and is confused. It's over complicating what you need for the first exercise:

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


#20

I used the code, and it worked, but it didn't return anything. Why?


#21

Hi try to call the function isEven with a number.