for循环中sum赋值为什么不直接写成sum += parseInt(sourceArr[i].substr(3));
sum += parseInt(sourceArr[i].substr(sourceArr[i].indexOf(":")+1));
可以替换成sum += parseInt(sourceArr[i].substr(3)); 结果是正常呢
这样有什么不好的地方吗?
2016-07-06
var present=new Date()
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
document.write(present.getFullYear()+"年"+(present.getMonth()+1)+"月"+present.getDate()+"日"+weekday[present.getDay()]+"<br/>");
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
document.write(scoreStr.split(";") + "<br />");
var sourceArr = scoreStr.split(";");
var sum=0;
for(var i=0;i<sourceArr.length;i++){
/*sum += parseInt(sourceArr[i].substr(sourceArr[i].indexOf(":")+1));*/
sum += parseInt(sourceArr[i].substr(3));
}
document.write("平均成绩:"+Math.round(sum/(sourceArr.length))+"<br/>");
标记为黑的这样写可以吗?结果是对的
举报