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?
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?
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.
Well you've already used if and else. If I'm not mistaken, if/else if/else statements allow for 3 different outcomes.
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.
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.
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
Hope this helped!
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.
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:
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)
. 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.
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.
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