isNaN


#1

I put the following code into my console and it worked perfectly yet an error message pops up when I submit it to code academy

var isEven = function(number) {
if (number % 2 === 0){
return “the number is even”;
}else if(isNaN(“number”)){
return “user input…not a number”;
}else {
return false;
}
};
isEven();

wondering if chrome is more forgiving of an error or if I do have the correct code.

Thanks


#2

isEven();

you didn’t pass any argument for isEven() function.

if (number % 2 === 0){

what is number?


#3

your right I didn’t assign a value to the number that is passed in as a condition of the function isEven. I could have made a global variable and set number to something like

var number = 2;
can’t seem to access the course on scope (didn’t hit all of the prerequisites) not sure the java script for dummies track is compatible with the javascript course so my understanding is quite limited. I thought that local scope could access the global scope so would this be the fix?

I see your point an oversight on my part I should have passed it the variable number as defined in the global scope.

Thanks for your feedback. I am going to give your suggestions a try.


#4

I tried the following code and it continues to say your function returns true for 3 so yeah not sure

var number = 2;

var isEven = function(number) {
if (number % 2 === 0){
return “the number is even”;
}else if(isNaN(“number”)){
return “user input…not a number”;
}else {
return false;
}
};
isEven(number);


#5

else if(isNaN("number"))

number is not a string value. so we dont need ""

and local scope could access the global scope if you didn’t take the parameter named number.

As you took number as parameter so inside the function number was nothing because you passed nothing for that parameter.


#6

Thanks for your help. Think it was due to the hour I missed to remove “” from number. Your answer was dead on. Thanks for going through it and helping me see what I was doing wrong.


#7

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