2: If/Else If/Else: returns true for 3!


#1

To start with, this was my code for 1: If/Else. I was following the syntax refresher template in the sidebar where it shows an if/else if/else template:

var number=prompt("Input a number.");
var clever="That's not an integer!";

var isEven = function(number) {
  if(number%2===0) {
      return true;
  }
  else if(number%2===1) {
      return false;
  }
  else {
      return clever;
  }
}

This was accepted, even if there was no output to the console. But then I wrote this for the second lesson, and I'm not sure why I'm getting an error. I've tried it with and without a fourth condition, and that doesn't make any difference.

var number=prompt("Input a number.");

var isEven = function(number) {
  if(number%2===0) {
      return "Your number is even.";
  }
  else if(number%2===1) {
      return "Your number is odd.";
  }
  else if(isNAN(number)) {
      return "That wasn't a number.";
  }
  else {
      return "Your number is not an integer";
  }
}

Thanks.


#2

Strip away the embellishment and follow the instructions exactly, including correct usage of isNaN() and this exercise is a walk in the park. Do it over with no improvisation and you should have no trouble getting it to work correctly.

Please post a link to the exercise if you still have questions.


#3

Thanks. I was confusing myself by building on what I'd had in the previous one. Also, I realized that I never actually called the function, so I changed that as well. It's working now.


#4

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

This is my code, but getting this error message:
"Oops, try again.
Your function returns true for 3!"

Can anyone point out my error? Please help


#5

The lesson checker must be evaluating this as a boolean. A non-empty string is truthy since it has some length. JavaScript interprets this as true.

Perhaps have your function return true for the even case, and false for the odd one.


#7