手记

JavaScript进阶篇之 7-23编程练习

<script type="text/javascript">
var curDate = new Date();
var year = curDate.getFullYear();
var month = curDate.getMonth()+1;
var day = curDate.getDate();
var weekday = curDate.getDay();
if(weekday != 0){
    weekday = "星期"+weekday; 
}else{
    weekday = "星期日";    
}
document.write(year+"年"+month+"月"+day+"日"+" "+weekday);
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scoreArr = scoreStr.split(";");

var scores = new Array();
var sum = 0,aver = 0;
for(var i in scoreArr){
    //将转化后的数组中的成绩取出来,并转化为Int型
    scores[i] = parseInt(scoreArr[i].substr(scoreArr[i].indexOf(":")+1));
}
for(var i in scores){
    sum = sum+scores[i];
}
aver = sum/scores.length;
document.write("--"+"班级平均分为:"+Math.floor(aver));
</script>
2人推荐
随时随地看视频
慕课网APP