循环案例
<!DOCTYPE html>
<html>
<head>
<title>js开始学习</title>
<script>
var time = prompt('请你输入现在的时间段');
if (time >= 0 && time <= 12) {
alert('中午好');
} else if (time > 12 && time <= 18) {
alert('傍晚好');
} else {
alert('深夜好,该吃夜宵啦');
}
</script>
</head>
<body>
</body>
</html>
for 循环
<!DOCTYPE html>
<html>
<head>
<title>js开始学习</title>
<script>
for (var i = 1; i <= 100; i++) {
console.log('继续学习JavaScript');
}
for (var number = 1; number <= 10; number++) {
if (number == 1) {
console.log('从一开始计数');
} else if (number == 10) {
console.log('到末尾啦');
} else {
console.log('当中输出的数字为:' + number);
}
}
</script>
</head>
<body>
</body>
</html>
for 循环案例
<!DOCTYPE html>
<html>
<head>
<title>js开始学习</title>
<script>
var sum = 0;
for (var i = 1; i <= 100; i++) {
sum += i;
}
console.log(sum / 100);
var singleNum = 0,
doubleNum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 0) {
doubleNum += i;
} else {
singleNum += i;
}
}
console.log('奇数和为:' + singleNum);
console.log('偶数和为:' + doubleNum);
var trippleNum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 3 == 0) {
trippleNum += i;
}
}
console.log('能被3整除的数和为:' + trippleNum);
</script>
</head>
<body>
</body>
</html>
for 循环案例之求学生平均成绩
<!DOCTYPE html>
<html>
<head>
<title>js开始学习</title>
<script>
var total = prompt('请输入班级的总人数');
var grade = 0;
var totalGrade = 0;
for (var i = 1; i <= total; i++) {
grade = prompt('请输入第' + i + '个同学的成绩');
totalGrade += parseInt(grade);
}
averageGrade = totalGrade / parseInt(total);
alert('全班的平均成绩为:' + averageGrade);
</script>
</head>
<body>
</body>
</html>
|