isNaN(number) not working, why?


#1


Exercise

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

var isEven = function(number) {
  // Your code goes here!
  if( number % 2 === 0 ) {
    return "Even";
  }
  else if( isNaN(number) ) {
    return "Its not a number";
  }
  else {
    return "Odd";
  }
};

isEven("why");


#2

in elseif statement you should use isEven and in the last line isEven statement write in the function


#3

What do you mean

The if statement have a condition of isEven and write in the function on the last line?

if( isEven ) {
// Rest of the code
};

isEven(number);

I don't think what I understood is right.


#4

sorry! in elseif statement you should use isEven('why') and isEven(3) write in above if statement.

var isEven = function(number) {
  // Your code goes here!
  isEven(3);
  if( number % 2 === 0 ) {
    return true;
  }
  else if( isEven('why') ) {
    return "Its not a number";
  }
  else {
    return false;
  }
};

#5

Your code works but why would the function called from within?

I tried again using the code I posted but changing the return value to true and false for if and else statements, respectively and it worked.

Anyway, thank you so much!


#6

yes, you are right, you don't need to call the function in the method.if you removed isEven(3); there is no change your code.


#7

I don't get it ?? if I replace true and false with even and odd, why is it showing "Your function returns for 3"


#8

The instructions as I recall start off with just true or false as returns, then in the next lesson we are introduced to the else if condition. Seasoned learners will see this as odd, but we must not replace the learner's experience with our own priorities.

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

Given this as passing the previous lesson learner's sometimes struggle with the next instruction; seasoned learners come along and add to their quandary.

Since it's in the instructions, I find it best to just humor the author and follow the instrucitons. Before the else we would insert the else if, regardless that it seems out of place from a refactoring perspective. It doesn't matter, either way.

var function isEven(x) {
    if (x % 2 === 0) {
        return true;
    }
    else if (isNaN(x)) {
        return "Not a Number";
    } else {
        return false;
    }
}

#12

I don't get it either, maybe its how they programmed the course(?). I'm not an expert myself and I have no clue as to why it is showing "Your function returns for 3".

If I were to inspect the code, I'm quite certain it does not return true for all.


#13

When the name of a function includes the term, is, it is a fair assumption that it will be returning a boolean.

isEven => true or false

#14

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