More on Control Flow in JS: 2. If / else if / else: Evaluating NaN in if condition


#1


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

Hi. This exercise asked the student to write the condition else if(isNaN(number)) in an isEven(number) function to handle non-number parameters. This condition was to be added to code I wrote previously, which worked fine.

var isEven = function(number) {
  if(!(number % 2)) { //if even, this should evaluate to true
//  if(number % 2 === 0) {
    return true;
  } else if(isNaN(number)) {
      return("This isn't a number yo.");
  } else {
    return false;
  }
};

However, the code triggered the error message: "Oops, try again. Make sure your if/else if/else statement returns a string when the function input isn't a number."

The reason is when a string is passed as an argument for the function, if(!(number % 2)) evaluates to true, when codeacademy expects the return value under the else if condition.

When number is a string, (number % 2) evaluates to NaN, which is a "falsy value," meaning it evaluates to false in boolean contexts. So:

if(!(number % 2)) becomes:
if(!false) becomes:
if(true) which returns true, giving the wrong return value and triggering the error.

The code I commented out underneath my if statement, however, works fine. I did it the other way because I was trying to make the code a tiny bit shorter. But it's probably more readable as: if(number % 2 === 0) anyway.

Hope this helps anyone who encounters the same error.

Source on falsy: https://developer.mozilla.org/en-US/docs/Glossary/Falsy


#2

That's some good ingenuity.

However we can definitely implement this in other ways including a similar variation of yours.

Nothing is stopping us from testing whether the variable is a number first. Because if it isn't then we can return "Is not a number" straight away. If it is a number then we could move on to determine whether the number is even or not.

var myNum = 37.99

var isEven = function(number) {
  if(isNaN(number)){
    return "You did not enter a number";
  }
  
  else if(!(number % 2)){
    return true;
  }
  
  else {
    return false;
  }
  
};

isEven(myNum);

Else if and else could even be swapped around if you'd like e.g.

var myNum = 37.99

var isEven = function(number) {
  if(isNaN(number)){
    return "You did not enter a number";
  }
  
  else if(number % 2){
    return false;
  }
  
  else {
    return true;
  }
  
};

isEven(myNum);

Just thought I'd share some more perspectives to the same problem :slight_smile:


#3

Oh that's a really good point, I like the last version you posted.

It does make sense to check for NaN before even evaluating odd or even. (I had decided for minimum change to existing code to see how codeacademy would respond.)

Good to know that modulo statements include decimal portions in the evaluated remainder.

Thanks for the response!


#4

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