Home > Web Front-end > JS Tutorial > body text

Two ways to determine odd and even numbers in JavaScript

yulia
Release: 2018-10-22 11:42:46
Original
45545 people have browsed it

JavaScript is an essential part of front-end development, so do you know how to use JS to determine whether a number is odd or even? This article will share with you two methods of judging odd and even numbers in JS. It has certain reference value. Interested friends can refer to it.

Method 1: Use the if function to determine whether the number is divisible by 2

Description: Use the if function to determine whether the number is divisible by 2. If it is divisible, an even number will pop up. If If not, it is an odd number. The specific code is as follows:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
 </head>
 <body>
  <input type="text" id="input1" value="" />
        <button id="btn">点击</button>
 </body>
 <script type="text/javascript">
  btn.onclick=function(){
        if(input1.value%2==0){
            alert("这是个偶数");
        }else
            alert("这是个奇数");
    }
 </script>
</html>
Copy after login

The effect is as shown in the picture. Obviously 33 is an odd number

Two ways to determine odd and even numbers in JavaScript

Two ways to determine odd and even numbers in JavaScript

Method 2: Use the ternary operator to determine whether the number is divisible by 2

Description: Get the input value, and use the ternary operator to determine whether the number is divisible by 2. If so, Then an even number will pop up. If not, it will be an odd number. The specific code is as follows:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <title></title>
 </head>
 <body>
  <input type="text" id="input1" value="" />
  <input type="button" value="判断" onClick="aa()">
 </body>
 <script type="text/javascript">
   function aa(){
      alert( (input1.value%2 ==0) ?"偶数":"奇数");  //判断是否能整除2
     }
 </script>
</html>
Copy after login

The above introduces you to the two methods of JS to determine whether a number is odd or even. The principle is the same, but the implementation method is different. Beginners You can try it yourself, I hope this article will be helpful to you!

For more related video tutorials, please visit JavaScript Tutorial

The above is the detailed content of Two ways to determine odd and even numbers in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!