- 创建二维数组student,输入3名学生的4门成绩,计算出总分,平均分、最高分,最低分,输出显示如下
姓名 科目1 科目2 科目3 科目4 总分 平均分
小明 99 90 72 83
学生2 61 93 56 93
学生3 68 83 67 98
最高分
最低分
姓名 科目1 科目2 科目3 科目4 总分 平均分
小明 99 90 72 83
学生2 61 93 56 93
学生3 68 83 67 98
最高分
最低分
<script type="text/javascript">
function test(){
var studList = [];
var maxScore = 0;
var minScore = 0;
studList.push({"name":"小明","s1":99,"s2":90,"s3":72,"s4":83,"total":0,"avg":0});
studList.push({"name":"学生2","s1":66,"s2":70,"s3":82,"s4":73,"total":0,"avg":0});
studList.push({"name":"学生3","s1":91,"s2":80,"s3":73,"s4":93,"total":0,"avg":0});
for(var i=0;i<studList.length;i++){
studList[i].total = studList[i].s1 + studList[i].s2 + studList[i].s3+studList[i].s4;
studList[i].avg = studList[i].total/4;
//console.log(studList[i]);
}
minScore = studList[0].total;
for(var i=0;i<studList.length;i++){
if(studList[i].total>maxScore){
maxScore = studList[i].total;
}
if(studList[i].total<minScore){
minScore = studList[i].total;
}
console.log(studList[i]);
}
console.log("最高分=" + maxScore);
console.log("最低分=" + minScore);
}
test();
</script>