js判断奇偶数

JavaScript026

js判断奇偶数,第1张

<!DOCTYPE html>

<html lang="en">

<head>

<title>奇偶数</title>

</head>

<body>

<input type=text>

<input type=button value="判断" onclick="chk(value)">

<p id="demo"></p>

<script>

function chk(num) {

var inputs = document.getElementsByTagName('input')[0]

var value = inputs.value

num = value

console.log(num)

alert((num % 2 == 0) ? "偶数" : "奇数") //判断是否能整除2

}

</script>

</body>

</html>

function jishu(num)

{

  console.log('我是奇数:'+num)

}

function oushu(num)

{

  console.log('我是偶数:'+num)

for(var i=1i<=100i++)

{

    if(i%2==0)

        {

            oushu(i)

        }

    else

        {

            jishu(i)

        }

}

0单独拿出来做一种情况进行判断。

1-30的情况下,对2取余数,即可判断其是否是偶数或奇数了。

<!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 runat="server">

<title>无标题页</title>

<script language="javascript" type="text/javascript">

//检查 奇偶数

function checkNum()

{

var intNum1 = document.getElementById("txtNum").value

if(intNum1 == 0)

{

alert("是0,是奇数还是偶数?")

}

else

{

var result = intNum1 % 2

if(result == 0)

{

alert(" " + intNum1 + " 是偶数!")

}

else

{

alert(" " + intNum1 + " 是奇数!")

}

}

}

</script>

</head>

<body>

<div>

<input id="txtNum" type="text" /><input id="btn1" type="button" value="计算" onclick="javascript:checkNum()" />

</div>

</body>

</html>