Find if variable is divisible by 2

JavascriptModuloDivide

Javascript Problem Overview


How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.

Javascript Solutions


Solution 1 - Javascript

Use modulus:

// Will evaluate to true if the variable is divisible by 2
variable % 2 === 0  

Solution 2 - Javascript

Seriously, there's no jQuery plugin for odd/even checks?

Well, not anymore - releasing "Oven" a jQuery plugin under the MIT license to test if a given number is Odd/Even.

Source code is also available at http://jsfiddle.net/7HQNG/

Test-suites are available at http://jsfiddle.net/zeuRV/

(function() {
    /*
     * isEven(n)
     * @args number n
     * @return boolean returns whether the given number is even
     */
    jQuery.isEven = function(number) {
        return number % 2 == 0;
    };

    /* isOdd(n)
     * @args number n
     * @return boolean returns whether the given number is odd
     */
    jQuery.isOdd = function(number) {
        return !jQuery.isEven(number);
    };
})();​

Solution 3 - Javascript

You don't need jQuery. Just use JavaScript's Modulo operator.

Solution 4 - Javascript

You can use the modulus operator like this, no need for jQuery. Just replace the alerts with your code.

var x = 2;
if (x % 2 == 0)
{
  alert('even');
}
else
{
  alert('odd')
}

Solution 5 - Javascript

You can do it in a better way (up to 50 % faster than modulo operator):

odd: x & 1 even: !(x & 1)

Reference: High Performance JavaScript, 8. ->Bitwise Operators

Solution 6 - Javascript

You can also:

if (x & 1)
 itsOdd();
else
 itsEven();

Solution 7 - Javascript

if (x & 1)
 itIsOddNumber();
else
 itIsEvenNumber();

Solution 8 - Javascript

var x = 2;
x % 2 ? oddFunction() : evenFunction();

Solution 9 - Javascript

Hope this helps.

let number = 7;

if(number%2 == 0){      
  
  //do something;
  console.log('number is Even');  
 
}else{

  //do otherwise;
  console.log('number is Odd');

}

Here is a complete function that will log to the console the parity of your input.

const checkNumber = (x) => {
  if(number%2 == 0){      

    //do something;
    console.log('number is Even');  

  }else{

    //do otherwise;
    console.log('number is Odd');

  }
}

Solution 10 - Javascript

Please write the following code in your console:

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

Please Note: It will return true, if the entered number is even otherwise false.

Solution 11 - Javascript

Use Modulus, but.. The above accepted answer is slightly inaccurate. I believe because x is a Number type in JavaScript that the operator should be a double assignment instead of a triple assignment, like so:

x % 2 == 0

Remember to declare your variables too, so obviously that line couldn't be written standalone. :-) Usually used as an if statement. Hope this helps.

Solution 12 - Javascript

array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

array.each { |x| puts x if x % 2 == 0 }

ruby :D

2 4 6 8 10

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsadmicrowaveView Question on Stackoverflow
Solution 1 - JavascriptAndy EView Answer on Stackoverflow
Solution 2 - JavascriptAnuragView Answer on Stackoverflow
Solution 3 - JavascriptMike AtlasView Answer on Stackoverflow
Solution 4 - JavascriptwsanvilleView Answer on Stackoverflow
Solution 5 - JavascriptKlapaucjusz TFView Answer on Stackoverflow
Solution 6 - JavascriptAlex K.View Answer on Stackoverflow
Solution 7 - JavascriptsagarView Answer on Stackoverflow
Solution 8 - JavascriptPablo CabreraView Answer on Stackoverflow
Solution 9 - JavascriptEdgar256View Answer on Stackoverflow
Solution 10 - JavascriptDeepak UpadhyayView Answer on Stackoverflow
Solution 11 - JavascriptSean Tank GarveyView Answer on Stackoverflow
Solution 12 - JavascriptSteveOView Answer on Stackoverflow