6

I am supposed to be getting a number that is divisible by two and I am doing that. I am not sure why my code is not working. I'm doing this in a course to learn javascript. There error I get is this:

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.

The question is this:

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()!

My code

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

What am I doing wrong?

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
Becky
  • 2,283
  • 2
  • 23
  • 50

3 Answers3

9

Try this

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

console.log(isEven(4));
console.log(isEven(3));
console.log(isEven(6));

more beautiful in one line

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

console.log(isEven(4));
console.log(isEven(3));
console.log(isEven(6));
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
3

The other answers are correct at the time I type this but to explain

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

% this is the modulus division operator. So it returns a value. Modulus tells you the remainder left over after division. So 4 modulus 2 returns 0 since there is nothing left over. Hence why you need to check against the returned value

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

If there is no remainder it is an even number because 2 divided it evenly with nothing left over. So 3 modulus 2 returns 1 since 2 divides three once and leaves 1 left over.

As per the comment below (seems it was deleted), it seems when talking in negative and positive numbers there is a difference between modulus and remainder but for strictly speaking javascript operator this is what it is defined as:

The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2. There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

Also you have hard coded 4 into the if statement so it will always return true! The parameter of the function is number.

var isEven = function (number) {
    // Your code goes here!
    if (number % 2 == 0) {
        return true;
    }
    else {
        return false;
    }
};
AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
  • Thanks for the great answer! Why were others saying to use the number parameter. I never had that defined, so how could it be true if `number % 2 === 0' . I tried this and it was correct. I just don't get why. – Becky Nov 11 '15 at 16:03
  • @Becky See in the function definition line "var isEven = function(number) {", inside the parentheses after function is where you define the inputs or parameters to a function. You defined a parameter named number. This is so you can pass in a value and test it against the function. So anytime you do this: isEven(30) inside the function the parameter number now holds the value 30. Also no problem! Happy to help =] – AtheistP3ace Nov 11 '15 at 16:05
  • Gotcha, so I am just making an if statement and later if I give `number` a value, it will test it. – Becky Nov 11 '15 at 16:07
  • Correct. You if statement will always return true because 4 modulus 2 will always return 0, Using the defined parameter number makes the function dynamic since its outcome depends on a variable that can change every time the function is called. – AtheistP3ace Nov 11 '15 at 16:12
  • @Becky if you are using something like Codecademy, they will insert a number in the parameter for you, no need to call the function yourself. – nihiser Nov 11 '15 at 16:19
1

In if statement 4%2 gives 0 and that gives false in if statement, that's why it is not working.

change if statement by

if(4%2==0)
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Mukesh Gupta
  • 1,373
  • 3
  • 17
  • 42