How to determine if a number is odd in JavaScript

后端 未结 27 1632
一向
一向 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:37

    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.

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

    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:

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

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

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

    Just executed this one in Adobe Dreamweaver..it works perfectly. i used if (isNaN(mynmb))

    to check if the given Value is a number or not, and i also used Math.abs(mynmb%2) to convert negative number to positive and calculate

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    
    </head>
    <body bgcolor = "#FFFFCC">
        <h3 align ="center"> ODD OR EVEN </h3><table cellspacing = "2" cellpadding = "5" bgcolor="palegreen">
            <form name = formtwo>
                <td align = "center">
                    <center><BR />Enter a number: 
                        <input type=text id="enter" name=enter maxlength="10" />
                        <input type=button name = b3 value = "Click Here" onClick = compute() />
                          <b>is<b> 
                    <input type=text id="outtxt" name=output size="5" value="" disabled /> </b></b></center><b><b>
                    <BR /><BR />
                </b></b></td></form>
            </table>
    
        <script type='text/javascript'>
    
            function compute()
            {
              var enter = document.getElementById("enter");
              var outtxt = document.getElementById("outtxt");
    
              var mynmb = enter.value;
              if (isNaN(mynmb)) 
              { 
                outtxt.value = "error !!!"; 
                alert( 'please enter a valid number');
                enter.focus();
                return;
              }
              else 
              { 
                 if ( mynmb%2 == 0 ) { outtxt.value = "Even"; }  
                 if ( Math.abs(mynmb%2) == 1 ) { outtxt.value = "Odd"; }
              }
            }
    
        </script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 10:42

    Many people misunderstand the meaning of odd

    • isOdd("str") should be false.
      Only an integer can be odd.
    • isOdd(1.223) and isOdd(-1.223) should be false.
      A float is not an integer.
    • isOdd(0) should be false.
      Zero is an even integer (https://en.wikipedia.org/wiki/Parity_of_zero).
    • isOdd(-1) should be true.
      It's an odd integer.

    Solution

    function isOdd(n) {
    
      // Must be a number
      if (isNaN(n)) {
        return false;
      }
    
      // Number must not be a float
      if ((n % 1) !== 0) {
        return false;
      }
    
      // Integer must not be equal to zero
      if (n === 0) {
        return false;
      }
    
      // Integer must be odd
      if ((n % 2) !== 0) {
        return true;
      }
    
      return false;
    }
    

    JS Fiddle (if needed): https://jsfiddle.net/9dzdv593/8/

    1-liner

    Javascript 1-liner solution. For those who don't care about readability.

    const isOdd = n => !(isNaN(n) && ((n % 1) !== 0) && (n === 0)) && ((n % 2) !== 0) ? true : false;
    
    0 讨论(0)
  • 2020-11-27 10:43

    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.

    0 讨论(0)
  • 2020-11-27 10:43
       <script>
            function even_odd(){
                var num =   document.getElementById('number').value;
    
                if ( num % 2){
                    document.getElementById('result').innerHTML = "Entered Number is Odd";
                }
                else{
                    document.getElementById('result').innerHTML = "Entered Number is Even";
                }
            }
        </script>
    </head>
    <body>
        <center>
            <div id="error"></div>
            <center>
                <h2> Find Given Number is Even or Odd </h2>
                <p>Enter a value</p>
                <input type="text" id="number" />
                <button onclick="even_odd();">Check</button><br />
                <div id="result"><b></b></div>
            </center>
        </center>
    </body>
    
    0 讨论(0)
提交回复
热议问题