Help me please with this code


#1

var isEven = function(number) {
if(isNaN(number) == true){
if(number % 2 == 0){
return "Thank you for your input"
}
else{
return "Please insert a number"
}
}
};

Whats wrong with this code?


#2

I seen someone ask about this a while ago. Shouldn't your second "if" be "else if"? There might be more, but that's what is standing out most to me.


#3

Thats the thing I dont understand, why should I use "else if"?


#4

Well you've already used if and else. If I'm not mistaken, if/else if/else statements allow for 3 different outcomes.


#5

But they wanted only two outcomes right?


#6

So, I changed the outcomes to three and rewrote my code to this:

var isEven = function(number) {
// Your code goes here!
if(isNaN(number) == true){
return "Thank you for your input"
}
else if(number % 2 == 0){
return "Your number is even"
}
else{
return "Please insert a number"
}
};

And its giving this "Oops, try again.
Your function returns true for 3!" as the new error.


#7

From what I just tried, you don't need "== true" for the first part, and the second part is missing an "=". It also wants you to return true or false, so you can't have custom text for the last two.

Edit: This seems to be the minimum of what will pass. After trying some more, it seems it doesn't matter whether you use 3 "=", or add "== true" to the first part. Which is now confusing me more than before. :confused:


#8
var isEven = function(number) {
  // Your code goes here!
  if(number%2===0){
      return true;
  }
  else if(isNaN(number)){
      return "That are not a number";  
  } 
  else {
      return false;
  }
};
isEven(2);

Here is my code and it worked, you can compare yours to mine :smile:
Hope this helped!


#9

Else if is a secondary area within your code, like a bracket in a sentence and is often used for checking the number rather than using another "if" statement which can confuse the computer if there is an else statement.


#10

Ok let's wrap up some of the information in this thread:

@eternallocket if you have a close look at this:

 if(isNaN(number) == true){      
    if(number % 2 == 0){
        return "Thank you for your input"
    }
    else{
        return "Please insert a number"
    }
}

you can see that this is an if/else nested in an if, which would be a perfectly valid approach -> check if it is a number if it is -> determine whether it is even or odd. There are just 2 problems with it:

  1. isNaN(number) == true this means you only go inside if number is not a number, which is kind of the opposite of what you want to do. Workarounds would be the use of ! (not operator) as in isNaN(number) !== true or simply isNaN(number).
  2. As @eternallocket also mentioned the exercise expects 3 different outputs. So you need to add another case. Whether this is an else if or an else or just a return doesn't really matter:

         if(isNaN(number)){      
            if(number % 2 == 0){
                return "Thank you for your input"
            }
            else{
                return "Please insert a number"
            }
        }
        else{
            return "The input is not a number";
        }

or

         if(isNaN(number)){      
            if(number % 2 == 0){
                return "Thank you for your input"
            }
            else{
                return "Please insert a number"
            }
        }
            return "The input is not a number";

If you can count brackets and indent it carefully so that you don't miss the overview this is perfectly fine, using else if just saves you a level of indentation so when you would have 2 or more levels of nesting it might be worth considering else if instead and if you have a look at this:

it is nothing other than nesting conditions.

So the last thing is that the output is expected to be of a certain type:

true for even
false for odd
and any String in case of NaN

What you're currently using is a string for all of them. If you now think of the test cases as something like this:

if(isEven(input))

And have a look at this:
http://www.sitepoint.com/javascript-truthy-falsy/
then you see that non-empty strings are treated truthy so even your false message might trigger the "even" result. Hope this makes it a little clearer.


#11

Thank you for the explanation. I'm glad I went with the else if version, I might not have noticed any indentation problems. I thought about mentioning you, since I'm pretty sure you were on the last thread like this one.


#12

It's quite simple

var isEven = function(number) {
// Your code goes here!
if(number % 2===0) {
return true;
}else {
return false;
}
}
isEven(6);

Use this it will work