如何在 JavaScript 中确定一个数字是否为奇数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5016313/
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 15:15:25  来源:igfitidea点击:

How to determine if a number is odd in JavaScript

javascript

提问by Jannie Theunissen

Can anyone point me to some code to determine if a number in JavaScript is even or odd?

谁能指出一些代码来确定 JavaScript 中的数字是偶数还是奇数?

回答by Chii

Use the below code:

使用以下代码:

function isOdd(num) { return num % 2;}
console.log("1 is " + isOdd(1));
console.log("2 is " + isOdd(2));
console.log("3 is " + isOdd(3));
console.log("4 is " + isOdd(4));

1 represents an odd number, while 0 represents an even number.

1代表奇数,0代表偶数。

回答by 0x499602D2

Use the bitwise ANDoperator.

使用按位运算AND符。

function oddOrEven(x) {
  return ( x & 1 ) ? "odd" : "even";
}

function checkNumber(argNumber) {
  document.getElementById("result").innerHTML = "Number " + argNumber + " is " + oddOrEven(argNumber);
}
 
checkNumber(17);
<div id="result" style="font-size:150%;text-shadow: 1px 1px 2px #CE5937;" ></div>

If you don't want a string return value, but rather a boolean one, use this:

如果你不想要一个字符串返回值,而是一个布尔值,使用这个:

var isOdd = function(x) { return x & 1; };
var isEven  = function(x) { return !( x & 1 ); };

回答by TNC

You could do something like this:

你可以这样做:

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

回答by CloudyMarble

function isEven(x) { return (x%2)==0; }
function isOdd(x) { return !isEven(x); }

回答by Isaac Fife

Do I have to make an array really large that has a lot of even numbers

我是否必须制作一个非常大的数组,它有很多偶数

No. Use modulus (%). It gives you the remainder of the two numbers you are dividing.

否。使用模数 (%)。它为您提供要除的两个数字的余数。

Ex. 2 % 2 = 0 because 2/2 = 1 with 0 remainder.

Ex2. 3 % 2 = 1 because 3/2 = 1 with 1 remainder.

Ex3. -7 % 2 = -1 because -7/2 = -3 with -1 remainder.

This means if you mod any number x by 2, you get either 0 or 1 or -1. 0 would mean it's even. Anything else would mean it's odd.

这意味着如果您将任何数字 x 乘以 2,您将得到 0 或 1 或 -1。0 表示它是偶数。其他任何事情都意味着它很奇怪。

回答by s0d4pop

This can be solved with a small snippet of code:

这可以通过一小段代码解决:

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

Hope this helps :)

希望这可以帮助 :)

回答by pb2q

Like many languages, Javascript has a modulus operator %, that finds the remainder of division. If there is no remainder after division by 2, a number is even:

像许多语言一样,Javascript 有一个模运算符%,它可以找到除法的余数。如果除以 2 后没有余数,则数字为偶数:

// this expression is true if "number" is even, false otherwise
(number % 2 == 0)

This is a very common idiom for testing for even integers.

这是测试偶数的一个非常常见的习惯用法。

回答by Katia Punter

With bitwise, codegolfing:

使用按位,codegolfing:

var isEven=n=>(n&1)?"odd":"even";

回答by dgh

A simple function you can pass around. Uses the modulo operator %:

您可以传递的简单函数。使用模运算符%

var is_even = function(x) {
    return !(x % 2); 
}

is_even(3)
false
is_even(6)
true

回答by Abdennour TOUMI

Use my extensions :

使用我的扩展:

Number.prototype.isEven=function(){
     return this % 2===0;
};

Number.prototype.isOdd=function(){
     return !this.isEven();
}

then

然后

var a=5; 
 a.isEven();

==False

==假

 a.isOdd();

==True

==真

if you are not sure if it is a Number , test it by the following branching :

如果您不确定它是否为 Number ,请通过以下分支进行测试:

if(a.isOdd){
    a.isOdd();
}

UPDATE :

更新 :

if you would not use variable :

如果您不使用变量:

(5).isOdd()

Performance :

表现 :

It turns out that Procedural paradigm is better than OOP paradigm . By the way , i performed profiling in this FIDDLE. However , OOP way is still prettiest .

事实证明,过程范式优于面向对象范式。顺便说一下,我在这个 FIDDLE 中进行了 分析。但是,OOP 方式仍然是最漂亮的。

enter image description here

在此处输入图片说明