2/14 curious regarding console.log


#1

So I understand that the error here is that I have written "console.log" and not "return", but I am curious as to why console.log is printing out regardless of whether or not the "else if"-statement is true.

For the example below it prints out the "console.log" on one line, then "true" on the next. If the input is not a number it will print out the "console.log" on both lines.

Any insights regarding the logic behind console.log printing out text written in code that should not be read?

`var isEven = function(number) {
  // Your code goes here!
  
  if(number % 2 === 0)
  {
      return true;
  }
  
  else if(isNaN(number))
  {
      console.log("Enter a number, please");
  }
  
  else
  {
      return false;
  }
  
};

isEven(2)`

#2

i think this is codecademy running the check to see your code is correct, i slightly modified your code, html:

<input type="button" value="click me" onclick="isEven('string')">

javasript:

var isEven = function(number) {
  // Your code goes here!
  if(number % 2 === 0)
  {
      return true;
  }
  
  else if(isNaN(number))
  {
      console.log("Enter a number, please");
  }
  
  else
  {
      return false;
  }
  
};

i made a button to trigger the function, console.log is only shown when it is not a number. Why do you not first check if it is a number? and then by else if do: number % 2 === 0, to me that makes more sense. Maybe you can try a site like jsbin to try this?


#3

That's correct if you add the console.log at another point you could see that the code is run with numbers from 1 to 999 in even and odd order and for NaN itself. As return doesn't write to the screen you do not recognize them but console.log is an output to the screen so it becomes visible.