Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 755
Becky Avatar asked Nov 11 '15 15:11

Becky


People also ask

Does an if statement return true or false?

The test can be any expression that evaluates to a boolean value – true or false – value (boolean expressions are detailed below). The if-statement evaluates the test and then runs the body code only if the test is true.

Why is my IF statement always returning false?

An if statement does not evaluate whether the command inside its condition ran successfully. It will only check the value (in your case the return of your command) and cast it to a bool . Copy-Item does not return anything by default, and that's why your if statement is always false , because [bool]$null is $false .

Why does Excel return false?

FALSE Function in Excel. The FALSE function in Excel is a logical function that returns “FALSE” as an output when used in a blank cell. This function also does not take any arguments similar to the TRUE function in Excel.

Does or function return true or false?

If any of the given arguments is TRUE, the result would be TRUE. If both the arguments are FALSE, it will return FALSE. Note: Excel OR function can handle TRUE and FALSE within double quotes. For example, if you use =OR(“TRUE”,”FALSE”), it will return TRUE.


2 Answers

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));
like image 96
Oleksandr T. Avatar answered Oct 17 '22 09:10

Oleksandr T.


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;
    }
};
like image 27
AtheistP3ace Avatar answered Oct 17 '22 09:10

AtheistP3ace