2.If/else if/else. "Your function returns true for 3!"


#1

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/2?curriculum_id=506324b3a7dffd00020bf661#


When I run my code as posted below, I get the error message "Your function returns true for 3!"

I've searched the forums and compared my code to others with the same problem, and the only difference I can see is that many people put "if(number%===0) {" instead of my "if(number%=0) {", but when I add the == I get the error "SyntaxError: expected expression, got '=='."

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

#2

It looks like you're missing second operand after % (modulus) operator.
Change this line: if(number%===0) { to this: if(number % 2 === 0) {
As I said you somehow missed second operand, modulus operator needs two operands, one on left side (before) modulus sign and second - after it (right side).

But try to avoid using one = sign in if statements, one equal sign means that you're assigning value to something, == equal signs mean you're comparing two values, and === equal signs mean you're doing strict comparison, it checks values and types.


#3

That fixed it! Thank you so much!


#4

It's always good to help.


#5

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

This is the function which I had written and it does give an error telling "function returns false when number=3"? How should I modify my code?


#6

Well, you have number as a parameter meaning an input variable....but you're not using it in your function at all. Instead, you're just comparing random numbers (that has nothing to do with the input).

The desired algorithm of this function goes like this:
Is this number even? return true
Is this number odd? return false

Don't know where you pulled 20 and 10 from. The error seems confusing because it makes the function look like it's working and that Codecademy is just being funny again.


#7

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