How to determine if a number is odd in JavaScript

后端 未结 27 1649
一向
一向 2020-11-27 10:05

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

相关标签:
27条回答
  • 2020-11-27 10:49

    Use the bitwise AND operator.

    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 ); };
    
    0 讨论(0)
  • 2020-11-27 10:50

    You could do something like this:

    function isEven(value){
        if (value%2 == 0)
            return true;
        else
            return false;
    }
    
    0 讨论(0)
  • 2020-11-27 10:50

    This is what I did

    //Array of numbers
    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,32,23,643,67,5876,6345,34,3453];
    //Array of even numbers
    var evenNumbers = [];
    //Array of odd numbers
    var oddNumbers = [];
    
    function classifyNumbers(arr){
      //go through the numbers one by one
      for(var i=0; i<=arr.length-1; i++){
         if (arr[i] % 2 == 0 ){
            //Push the number to the evenNumbers array
            evenNumbers.push(arr[i]);
         } else {
            //Push the number to the oddNumbers array
            oddNumbers.push(arr[i]);
         }
      }
    }
    
    classifyNumbers(numbers);
    
    console.log('Even numbers: ' + evenNumbers);
    console.log('Odd numbers: ' + oddNumbers);
    

    For some reason I had to make sure the length of the array is less by one. When I don't do that, I get "undefined" in the last element of the oddNumbers array.

    0 讨论(0)
  • 2020-11-27 10:50

    I'd implement this to return a boolean:

    function isOdd (n) {
        return !!(n % 2);
        // or ((n % 2) !== 0).
    }
    

    It'll work on both unsigned and signed numbers. When the modulus return -1 or 1 it'll get translated to true.

    Non-modulus solution:

    var is_finite = isFinite;
    var is_nan = isNaN;
    
    function isOdd (discriminant) {
        if (is_nan(discriminant) && !is_finite(discriminant)) {
            return false;
        }
    
        // Unsigned numbers
        if (discriminant >= 0) {
            while (discriminant >= 1) discriminant -= 2;
    
        // Signed numbers
        } else {
            if (discriminant === -1) return true;
            while (discriminant <= -1) discriminant += 2;
        }
    
        return !!discriminant;
    }
    
    0 讨论(0)
  • 2020-11-27 10:52

    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
    
    0 讨论(0)
  • 2020-11-27 10:53
    function isEven(x) { return (x%2)==0; }
    function isOdd(x) { return !isEven(x); }
    
    0 讨论(0)
提交回复
热议问题