某班的成绩出来了,现在老师要把班级的成绩打印出来。
效果图:
XXXX年XX月X日 星期X--班级总分为:81
格式要求:
1、显示打印的日期。 格式为类似“XXXX年XX月XX日 星期X” 的当前的时间。
2、计算出该班级的平均分(保留整数)。
同学成绩数据如下:
"小明:87; 小花:81; 小红:97; 小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script type="text/javascript">
var thisDay = new Date();
//方法1:
var week = thisDay.getDay();
switch(week){
case 0: week = "星期日"; break;
case 1: week = "星期一"; break;
case 2: week = "星期二"; break;
case 3: week = "星期三"; break;
case 4: week = "星期四"; break;
case 5: week = "星期五"; break;
case 6: week = "星期六"; break;
}
document.write(thisDay.getFullYear() + "年" +( thisDay.getMonth() + 1 )+ "月" + thisDay.getDate() + "日" + " " + week);
//方法2:
var week = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
document.write(thisDay.getFullYear() + "年" +( thisDay.getMonth() + 1 )+ "月" + thisDay.getDate() + "日" + " " + week[thisDay.getDay()]);
var sum = 0;
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scoreArr = scoreStr.split(";");
//方法一:
for (var i = 0 ; i < scoreArr.length; i++) {
sum += parseInt(scoreArr[i].split(":")[1]);
}
document.write("平均成绩:"+ Math.floor(sum/scoreArr.length));
//方法二:
for (var i = 0 ; i < scoreArr.length; i++) {
sum +=parseInt(scoreArr[i].substring(scoreArr[i].indexOf(":") + ":".length)); //substring 可以用slice替换
}
document.write("平均成绩:"+ Math.floor(sum/scoreArr.length));
//方法三:
var scoreStr1 = scoreArr.join(":");
var scoreArr1 = scoreStr1.split(":");
for (var i = 1; i < scoreArr1.length; i +=2 ) {
sum += parseInt(scoreArr1[i]);
}
document.write("平均成绩:" + Math.floor(sum/scoreArr.length));
</script>
</body>
</html>