<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 使用3元表达式求3个数的最大值
function getMax(x, y, z) {
let max1, max2
max1 = x > y ? x : y
max2 = max1 > z ? max : z
return max2
}
x = prompt('x:')
y = prompt('y:')
z = prompt('z:')
console.log(getMax(x,y,z))
</script>
</body>
</html>
2、使用三种方式判断一个数是奇数还是偶数。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 用三种方式判断一个数是奇数还是偶数?(三元,if,switch)
function getInt() {
x = Number(prompt('请输入一个整数'))
for (i = 0; i < 1000; i++) {
if (Number.isInteger(x)) {
return x
break
}
x = prompt('请输入一个整数')
}
}
function J(b) {
if (b) {
console.log('是偶数')
} else {
console.log('是奇数')
}
}
function J1(x) {
return x % 2 == 0 ? true : false
}
function J2(x) {
if (x % 2 == 0) {
return true
} else {
return false
}
}
function J3(x) {
switch (x % 2) {
case 0:
return true
break
case 1:
return false
}
}
x = getInt()
J(J2(x))
</script>
</body>
</html>
3、打印倒三角形图案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 打印倒三角
for (let i = 5; i > 0; i--) {
let row = '';
for (let j = 0; j < i; j++) {
row += '*';
}
console.log(row);
}
</script>
</body>
</html>