If/Else If/Else - Javascript


#1




Message:

I entered this line of code, and it returns True for 3. I should just return as True with I assume a passing number. Can anyone help me solve this issue?

Thanks in advance!


var isEven = function(number) {
  // Your code goes here!
  if (number%2===0) {
      return true;
  } else if (isNaN(number)) {
      return "Is this right?";
  } else if (isEven) {
  return "Some string";
  } else if (isEven) {
  return "Another string";
  } else {
  return "Yet another string";
  }
  {
      return false;
  }
};
isEven(10);


#2

For one, why are you calling your function in your function? I'm surprised that it works at all. Secondly, why so many else ifs? Why not just check to see if it's even, and if not, return false?


#3

You should remove the 2 else ifs with the (isEven).
Then the else should not return "Yet another string", replace it with false.
Also, remove the { return false; } at the end.


#4

I do not quite clearly understand you guys. Can you help by providing a visual aid of some kind?

Thanks in advance!


#5

What are you trying do do with those lines? This code only needs to return True, False, and "Is this right?", so keep it simple!

These lines:
} else if (isEven) {
in particular are causing issues. They call the function isEven to see if it's true, but because you're still building the function isEven some strange stuff is happening.


#6

I will have to try, and see how it works; once I shorten up the code.

Thanks for the reply you guys!
Thanks in advance!


#7

Whats wrong in this code..
var isEven = function(number) {
// Your code goes here!
var isNonN = isNaN(number);
if( number % 2 ===0 )
return true;
else
return false;
else if(isNonN)
{return "Not a number";}
};


#8

Your if / else if / else syntax is missing a lot of semicolons. Check the examples and update your formatting.
You'll also want to change the order. It should go:

IF (condition1) {action if condition1 is true}
ELSE IF (condition2) {action if condition1 is false and condition 2 is true}
ELSE IF (condition3) {action if condition1 and 2 are false and condition3 is true}
...repeat as needed...
ELSE{action if no conditions are met}


#9

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