练习:使用for循环打印99乘法表(在页面内输出可用“document.write(‘’);”)。
代码:
<!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>
<script >
for (i = 1; i <= 9; i++) {
for (j = 1; j <= i; j++) {
document.write(i + "*" + j + "=" + i * j)
}
document.write('<br/>')
}
</script>
</body>
</html>
练习:使用While循环计算出100以内奇数相加之和。
代码:
<!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>
<script>
function Odd() {
var SumN = 0
for (i = 0; i <= 100; i++) {
if (i % 2 != 0) {
SumN += i
}
}
alert("100以内的奇数相加之和为" + SumN)
}
</script>
</head>
<body>
<input type="button" onclick="Odd()" value="点我" />
</body>
</html>