Becky
Becky

Reputation: 2275

if statement not returning true

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?

Upvotes: 6

Views: 3645

Answers (3)

AtheistP3ace
AtheistP3ace

Reputation: 9691

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

Upvotes: 3

Mukesh Gupta
Mukesh Gupta

Reputation: 1433

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)

Upvotes: 1

Oleksandr T.
Oleksandr T.

Reputation: 77502

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

Upvotes: 9

Related Questions