9.1 If/else: Why can't I call the isEven function in the if statement?


#1

Why can't I call my isEven function inside the if statement? like this:

var isEven = function(number) {

if (isEven(10) % 2 === 0) {
  return true;

}
else {
return false;
}
};

instead of having to do this:

var isEven = function(number) {

if (number % 2 === 0) {
  return true;

}
else {
return false;
}
};

isEven(10);


#2

@femibadejo
Your isEven is under construction (javascript doesn't know it exists yet before we close the brackets) inside of your isEven function a bit like local and global variable.


#3

Thanks for your response but even something like this doesn't work:

var isEven = function(number) {
return number;
};

if (number % 2 === 0) {
  return true;

}
else {
return false;
}

and it is similar to the one that runs in the " Introduction to functions in JS" module:

var quarter = function(number) {
return number/4;
};

if (quarter(12) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}

What am I missing?


#4

@femibadejo
Your second code is correct but your first one isn't correct:

number is outside of the isEven function so it's undefined.


#5

Should something like this work?

var isEven = function(number) {
return number;
};

if (isEven(10) % 2 === 0) {
  return true;

}
else {
return false;
}


#6

@femibadejo
Well, it will return the number (which is 10) and true. If you just want to put true, try removing the isEven function and place 10.


#7

That's the thing, it keeps saying there was a problem with my syntax.


#8

I've tried this on Mozilla Fire Bug and its return true

function isEven(number){
var num = number % 3;
if(num === 2){
return true;
} else{
return false;
}
}

isEven(2);

#9

@edoves

Hi,
If you have a problem, please post this in a new thread with your question, code, and error message.