More on Control Flow in JS


#1

var isEven = function(number) {
return number / 2;
if(isEven(12)/2 === 6) {
return true;
}
else {
return false;
}
};

TASK: Write an if / else statement inside the isEven function. It should return true; if the number it receives is evenly divisible by 2. Otherwise (else), it should return false;.Make sure to return - don't use console.log()!

I have been tried done this task a lot of times, but still don't understand what i'm doing wrong. Maybe someone help and explain me


#2

The problem I see with your code is that you're trying to use your function within your function. Your code should look something like this:

var isEven = function(number) {
return number/2; // also you want space between your 'number' and the unary operator '/'
} // here's where the change is <----- you didn't have the squiggle bracket showing the 'end' of your function.
if((isEven(12)%2) === 0) { /*another thing is that to check for even numbers, you just need to take the modulus of that number and if you get zero, it means the remainder is zero when you divide that number by 2 which shows that its an even number, otherwise, its an odd number*/
return true;
}
else {
return false;
}

Ultimately, your code should look like:

var isEven = function (number) {
return number/2;
}
if (isEven(12)%2 === 0) {
return true;
else {
return false;
}
//Hope this helps :slight_smile:
//Happy coding


#3

Hello! I cannot seem to get past this lesson! I have tried the above recommendations with no luck! I even tried to copy and paste it to only fail!

My code is currently:

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

}
else {
return false;
}
};
isEven(12);

Any help is appreciated! I think I understand what is going on in the code but the lesson will not take my input! I get the message

"Oops, try again. Looks like your function returns false when number = 2. Check whether your code inside the if/else statement correctly returns true if the number it receives is even."


#4

Hi I just ran your code and it work fine


And in what lesson are you?


#5

Sometimes if you ran your code, had an error and then corrected your code you get an error then you need to reset the lesson and paste in again or easier refresh the page does the trick most of the time :wink:


#6

Thank you!

I reset the code and tried it with good results! I never notice the "reset code" option before!!!!

Have a great week!