js小练习
1.题目:某班的成绩出来了,现在老师要把班级的成绩打印出来。
效果如下:xxx年xx月x日 星期x--班级总分为:81
格式要求:1.显示打印的日期。格式为类似“xxxx年xx月xx日 星期x”的当前的时间。
2.计算出该班级的平均分(保留整数)。
同学成绩数据如下: "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"
代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<script text="javascript">
var weekDay = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var date = new Date();
//alert(date.getFullYear()); 2017
//alert(date.getMonth()+1); 5
//alert(date.getDate()); 26
//alert(date.getDay()); 5
//alert(weekDay[date.getDay()-1]); Friday
var scoreStr = "Ming:87; Hua:81; Hong:97; Tian:76; Zhang:74; Li:94; Xi:90; Wu:76; Di:64; Man:76";
var nameArr = scoreStr.split(";");
var scoreSum = 0;
//alert(scoreArr.length); 10
for(var i=0; i<nameArr.length; i++){
var scoreArr = nameArr[i].split(":");
scoreSum = scoreSum + parseInt(scoreArr[1]);
}
var scoreAvg = scoreSum/nameArr.length;
document.write(date.getFullYear() + "." + parseInt(date.getMonth()+1) + "." + date.getDate() + " ");
document.write(weekDay[date.getDay()-1] + "<br />");
document.write("The total score is " + scoreSum + "<br />");
document.write("The average score is " + scoreAvg);
</script>
</head>
<body></body>
</html>
结果:
2017.5.26 Friday
The total score is 815
The average score is 81.5
注意:split、join和parseInt函数的使用