JS基础:数组for循环年龄案例
<script>let eage1 = []//定义一个空数组接受年龄for (let i = 1; i <= 5; i++) {let eage = prompt(`请输入第${i}个年龄`)//每次循环输入if (eage > 0 && eage < 100) {//满足0-100eage1.push(eage)//放入数组}}//1、依次打印数组输入进去的年龄document.write(eage1 + `<br>`)//2、打印所有成年人年龄for (let i = 0; i < eage1.length; i++) {if (eage1[i] > 18) {document.write(eage1[i] + `<br>`)}}//3、打印所有人总年龄let sum = 0for (let j = 0; j < eage1.length; j++) {sum += +eage1[j]}document.write(sum + `<br>`)//4、打印出所有人的平均年龄let average = 0average = sum / eage1.lengthdocument.write(average + `<br>`)//5、打印出年龄最大和最小的let max = eage1[0], min = eage1[2]for (let n = 0; n < eage1.length; n++) {if (eage1[n] > max) {max = eage1[n]}if (eage1[n] < min) {min = eage1[n]}}document.write(`输入年龄中最大的为${max}<br>`)document.write(`输入年龄中最小的为${min}<br>`)</script>