测试值是奇数还是偶数 [英] Testing whether a value is odd or even
问题描述
我决定用一个非常简单的算法创建简单的 isEven 和 isOdd 函数:
I decided to create simple isEven and isOdd function with a very simple algorithm:
function isEven(n) {
n = Number(n);
return n === 0 || !!(n && !(n%2));
}
function isOdd(n) {
return isEven(Number(n) + 1);
}
如果n具有某些参数,则可以,但在许多情况下都会失败。所以我开始创建强大的函数,为尽可能多的场景提供正确的结果,这样只测试javascript数量限制内的整数,其他一切都返回false(包括+和 - 无穷大)。注意零是偶数。
That is OK if n is with certain parameters, but fails for many scenarios. So I set out to create robust functions that deliver correct results for as many scenarios as I could, so that only integers within the limits of javascript numbers are tested, everything else returns false (including + and - infinity). Note that zero is even.
// Returns true if:
//
// n is an integer that is evenly divisible by 2
//
// Zero (+/-0) is even
// Returns false if n is not an integer, not even or NaN
// Guard against empty string
(function (global) {
function basicTests(n) {
// Deal with empty string
if (n === '')
return false;
// Convert n to Number (may set to NaN)
n = Number(n);
// Deal with NaN
if (isNaN(n))
return false;
// Deal with infinity -
if (n === Number.NEGATIVE_INFINITY || n === Number.POSITIVE_INFINITY)
return false;
// Return n as a number
return n;
}
function isEven(n) {
// Do basic tests
if (basicTests(n) === false)
return false;
// Convert to Number and proceed
n = Number(n);
// Return true/false
return n === 0 || !!(n && !(n%2));
}
global.isEven = isEven;
// Returns true if n is an integer and (n+1) is even
// Returns false if n is not an integer or (n+1) is not even
// Empty string evaluates to zero so returns false (zero is even)
function isOdd(n) {
// Do basic tests
if (basicTests(n) === false)
return false;
// Return true/false
return n === 0 || !!(n && (n%2));
}
global.isOdd = isOdd;
}(this));
任何人都可以看到上述问题吗?是否有更好的(即更准确,更快或更简洁而不被混淆)版本?
Can anyone see any issues with the above? Is there a better (i.e. more accurate, faster or more concise without being obfuscated) version?
有各种与其他语言相关的帖子,但我似乎无法找到ECMAScript的最终版本。
There are various posts relating to other languages, but I can't seem to find a definitive version for ECMAScript.
添加了代码审查标记,但我不是在代码审查之后。我简单地发布了代码,以便其他人可以看到我在哪里,而不是要审查的东西。到目前为止发布的答案似乎得到了。
A code review tag was added, but I'm not after a code review. I posted the code simply so others could see where I was up to, not as something to review. Answers posted so far seem to get that.
根据史蒂夫的回答,最终函数:
A final function, based on Steve's answer:
// Use abstract equality == for "is number" test
function isEven(n) {
return n == parseFloat(n)? !(n%2) : void 0;
}
// Use strict equality === for "is number" test
function isEvenStrict(n) {
return n === parseFloat(n)? !(n%2) : void 0;
}
任何不是数字的东西都会返回 undefined ,数字返回 true 或 false 。它可能是一个带有严格标志的函数,但我认为严格的比较并不是真正需要的。
Anything that is not a number returns undefined, numbers return either true or false. It could be one function with a strict flag, but I think the strict comparison is not really required.
推荐答案
使用模数:
function isEven(n) {
return n % 2 == 0;
}
function isOdd(n) {
return Math.abs(n % 2) == 1;
}
您可以检查Javascript中的任何值是否都可以强制转换为以下数字:
You can check that any value in Javascript can be coerced to a number with:
Number.isFinite(parseFloat(n))
此检查最好在 isEven
和 isOdd
函数之外完成,因此您不必在两个函数中复制错误处理。
This check should preferably be done outside the isEven
and isOdd
functions, so you don't have to duplicate error handling in both functions.
这篇关于测试值是奇数还是偶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!