2. if / else if/ else


#1

Need help. Not sure what was wrong with my code here:

var isEven = function(number) {
  if (number % 2 === 0){
      return true;
  }
  else if (isNaN(undefined)){
      return "You need to enter a number";
  }
  else {
      return false;
  }
};
isEven("berry")

Exercise result says: "Oops, try again.
Your function returns true for 3!"

--- I changed "undefined" to "number" and passed...I have no idea why:

var isEven = function(number) {
  if (number % 2 === 0){
      return true;
  }
  else if (isNaN(number)){
      return "You need to enter a number";
  }
  else {
      return false;
  }
};
isEven("berry")

thx


#2

because the argument you are passing into the function is defined as number.

"berry" is the number. Number doesn't have to be number it is just how the key is defined in this instance.


#3

as undefined is not a number isNaN(undefined) will always be true so unless your parameter number is even you'll always pick the NaN case. Now if you have a look at this:
http://www.sitepoint.com/javascript-truthy-falsy/
you can see that non-empty strings are treated as true, so when the test function expected a falsy value it got a truthy value and therefore raised an error.


#4

thanks @haxor789 @puristaja and sorry for the delayed response


#5

I was having the same issue with this section. I played around and changed values for everything. But helped.