方法一
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// 定义变量 n
var n
// 定义函数 double
function double(n) {
// 判断条件是当n能被2求余为0时,代表是偶数,直接除以2可以得出偶数个数
if (n % 2 == 0) {
console.log( n = n / 2)
} else {
// 判断条件是当n不能被2求余为0时,代表是奇数,需要减一除以2可以得出偶数个数
console.log( n = (n - 1) / 2)
}
}
// 输入121,结果是60
double(121)
</script>
</html>
方法二
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
function ou(n) {
var k = 0 // 计数器
for(var a = 1; a <= n; a++) {
if(a % 2 === 0) {
k++
}
}
return k
}
var a = ou(121)
console.log(a);
</script>
</html>