Javascript 查找变量是否可以被 2 整除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2821006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:03:46  来源:igfitidea点击:

Find if variable is divisible by 2

javascriptmodulodivide

提问by sadmicrowave

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.

如何判断一个变量是否可以被2整除?此外,如果是,我需要做一个功能,如果不是,我需要做一个不同的功能。

回答by Andy E

Use modulus:

使用模数:

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

回答by Anurag

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

说真的,没有用于奇数/偶数检查的 jQuery 插件吗?

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

好吧,不再 - 在 MIT 许可下发布一个 jQuery 插件“Oven”来测试给定的数字是否为奇数/偶数。

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

源代码也可在http://jsfiddle.net/7HQNG/ 获得

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

测试套件可在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);
    };
})();?

回答by Mike Atlas

You don't need jQuery. Just use JavaScript's Modulooperator.

你不需要jQuery。只需使用JavaScript 的 Modulo运算符。

回答by wsanville

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

您可以像这样使用模数运算符,不需要 jQuery。只需将 替换为alerts您的代码。

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

回答by Klapaucjusz TF

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

你可以用更好的方式来做(比模运算符快 50%):

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

奇数:x & 1 偶数:!(x & 1)

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

参考:高性能 JavaScript,8. -> 按位运算符

回答by Alex K.

You can also:

你也可以:

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

回答by Pablo Cabrera

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

回答by sagar

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

回答by Edgar256

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

  }
}

回答by Deepak Upadhyay

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.

请注意:如果输入的数字甚至为假,它将返回真。