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?

7
  • 4
    4 should be number Commented Nov 11, 2015 at 15:46
  • So where would I define number? Commented Nov 11, 2015 at 15:46
  • number is the formal parameter to your function Commented Nov 11, 2015 at 15:47
  • number is the name you typed for the argument, so in the function after isEven(42), number contains 42, i.e. what you want to test against %2 Commented Nov 11, 2015 at 15:48
  • 1
    And also, 4 % 2 will give you 0 which is falsy. Commented Nov 11, 2015 at 15:48

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

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 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 =]
Gotcha, so I am just making an if statement and later if I give number a value, it will test it.
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.
@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.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.