在Javascript中测试偶数/奇数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Javascript中测试偶数/奇数相关的知识,希望对你有一定的参考价值。

One of those common tools that's easy to forget about is the Modulus operator (%), which returns the remainder of a division operation.

If you divide some number by two, a remainder of 0 indicates an even number, while a remainder of 1 indicates an odd number.
  1. var isEven = function(someNumber){
  2. return (someNumber%2 == 0) ? true : false;
  3. };
  4.  
  5. alert(isEven(64)); // Alerts "true".
  6.  
  7. alert(isEven(97)); // Alerts "false".

以上是关于在Javascript中测试偶数/奇数的主要内容,如果未能解决你的问题,请参考以下文章