5.1 if else


#1

What am I doing wrong?? :frowning:

var isEven = function(number) {
  // Your code goes here!
  var isEven = (4%2);
  if (4%2 === 0) {
    // Do something
    console.log(true);
    
} else if (isEven > 0) {
    console.log(false);
    
} else {    
    console.log("error");
}
};

#2

Look to

var isEven = function(number) {
  // Your code goes here!
  var isEven = (4%2);
  if (4%2 === 0) {
    // Do something
    console.log(true);
    ...
}

Your function should takes number as a parameter for function. So, when we call it

isEven(8);

it means that number = 8. Now you need to check does it number even or not. For this goal you need to number % 2 and check this result for 0. If result equals to zero do smth, if not smth else.

Please, review your function.


#3

thanks! But this doesnt work..

var isEven = function(number) {
// Your code goes here!
var isEven = (8);
if (8%2 === 0) {
// Do something
console.log(true);

} else if (isEven > 0) {
console.log(false);


#4

I think you might have forgotten to close the curly brace"}" for your if else statement.


#5

Of course, it's doesn't work, because you didn't check number for divisible by 2. Instruction says:

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;.

So, how it function works. Function has only one parameter number and we want to check does it number is even:

if (number % 2 === 0) {
   // Do something
} else {
   // Do something
}

You don't need any variables inside the function because it takes parameter and we work with that parameter inside the function.


#6

You don’t need to use console.log() but return. It should look like this.

var isEven = function(number) {
// Your code goes here!
if(number % 2 === 0) {
return true;
} else {
return false;
}
};
isEven(4);