2. if/else if/ else


#1

Hi,

I wonder what is wrong with this code.

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/2?curriculum_id=506324b3a7dffd00020bf661#

It says: "SyntaxError: expected expression, got keyword 'var'."

and:"Oops, try again. There was a problem with your syntax."

var isEven = function(number) 
var isNaN = function (number)
  // Your code goes here!
  {
   if (number %2 === 0) {return true;}
   
   else if (isNaN(number)) {return "Just a string. Not a number.";}
   
   else {return false;}
  
};

<


#2

Hi you function should be

var isEven = function(number) 
{


//here go your if, else if and else statement


}

just remove this part

var isNaN = function (number)

#3

It accepted the answer/code, but why it does not print anything to the black box? Like true or false etc.?


#4

You should call the function

isEven("type a number or a string")

#5

This code is qualified, so pls refer it:
var isEven = function(number) {
// Your code goes here!
if (number % 2 === 0) {
return true;
}
else if (isNaN(number)) {
return "Your input is not a number";
}
else { return false;}
};


#6

I've tried to run the this script exactly how you have it here, and I got "number" is not defined witch makes sense, then I replaced number with a number 2,4,5... like this else if (isNaN(2)); isEven(2) but I get " Make sure if/elseif/else statement returns a string, so in the end I left my script like this :slight_smile:

var isEven = function(number) {
if(number%2===0)
{
return true;
}
else if(isNaN)
{
return "this is not a string";
}
else
{
return false;
}
};
isEven();

and returns the string in the console/monitor "this is not a string" but in the bottom I get this "Oops, try again. Your function returns true for 3!"

I replaced isEven() to isEven(3), got same message, so if I replaced that with any number that doesn't dived 2 I get that message, but if I replace it with a number that is dived by 2, I get "true" in the console/monitor, and "Oops, try again. Your function returns true for 3!" in the bottom


#7

you need to have the user type in the number. so above the isEven funtion you need to set the variable number to a prompt.

var number = prompt("Type in a number");
var isEven = function(number) {
// Your code goes here!
if(number % 2 === 0) {
return true;
}
else if (isNaN(number)) {
return "not a number try again";
}
else {
return false;
}
};