isNaN difficulty


#1

Having the weirdest errors...I dont even know how or why. First, with out the else if and else statements commented out...it would print 3, then 1 - 1000 incremented by 2. with them commented out it only printed 3, 1, 2 and I still have no idea why but im positive its because of the isNaN(did some testing). But my biggest error is that even with this code it tells me the function will return true for 3, but it doesnt and actually returned "This number is not divisible by 2". So I am horrendously confused

var number = 3;

console.log(isNaN(number)); // For Testing Purposes

var isEven = function(number) {
console.log(number);
// Your code goes here!
if(isNaN(number)){
return "This is not a Number!" ;
}
/*
else if(number % 2 == 0){
return "This number is divisible by 2";
}
else{
return "This number is not divisible by 2";
}*/
};

isEven(number);


#2

Any string that isn't empty is true according to JS, so, because you are supposed to be returning false, it says that you're wrong.

EDIT: Also, you may have to remove your console.log() statements, and should definitely remove the number variable before the function.


#3

when i run this code

var number = 3;

console.log(isNaN(number)); // For Testing Purposes

var isEven = function(number) {
console.log(number);
// Your code goes here!
if(isNaN(number)){
return "This is not a Number!" ;
}
/*
else if(number % 2 == 0){
return "This number is divisible by 2";
}
else{
return "This number is not divisible by 2";
}*/
};

isEven(number);

it prints

false
3

because
console.log(isNaN(number));//it is false because 3 is number(isEven(number)=isEven(3))
console.log(number);// it prints 3

next when i run this code

var number = 3;

console.log(isNaN(number)); // For Testing Purposes

var isEven = function(number) {
console.log(number);
// Your code goes here!
if(isNaN(number)){
return "This is not a Number!" ;
}

else if(number % 2 === 0){
return "This number is divisible by 2";
}
else{
return "This number is not divisible by 2";
}
};

isEven(number);

it prints 'This number is not divisible by 2'
because 3 is divisible by 2'

if(isNaN(number)) means (3) is Not a Number

run this too it'll make sense.i have taken isEven("p")

var number = 3;

console.log(isNaN(number)); // For Testing Purposes

var isEven = function(number) {
console.log(number);
// Your code goes here!
if(isNaN(number)){
return "This is not a Number!" ;
}

else if(number % 2 === 0){
return "This number is divisible by 2";
}
else{
return "This number is not divisible by 2";
}
};

isEven("p");

any question?


#4

@craigb345 understands that, take a closer look at his code.

if(isNaN(number)){
    return "This is not a Number!" ;
}

#5

@rcodeman The function still works perfectly fine, even though it's a bad idea to have a variable of the same name (as the argument) above the function.


#6

I actually misinterpreted the instructions, the first and last conditions were supposed to stay as returning bool values. And only the new else if was to return a string. it worked perfectly, my apologies.


#7

I'm having similar problems. My question is why the 1-1000 numbers show up in the first place. Anyone know?


#8

Most likely, it is the automated tests that Codecademy runs on your code. (That's my best guess at least)