卡溜灰
2016-05-11 16:08
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
var today = new Date();
var month = today.getMonth() + 1 +"月";
var week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];
document.write(today.getFullYear()+"年"+month+today.getDate()+"日"+" "+week[today.getDay()]);
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scoreArr = scoreStr.split(";");
var average;
var num;
for (var i=0;i<scoreArr.length;i++){
var index = scoreArr[i].indexOf(':')+1;
num += parseInt(scorArr[i].substr(index));
average = num/soreArr.length;
}
document.write("--班级总分为:"+average);
</script>
</head>
<body>
</body>
</html>
现在只能正确输出日期,最后一个document.write语句的内容都不输出。试过将for循环注释掉之后会输出”--班级总分为“,但是实在找不到循环体的问题在哪里了。。。
注意单词和for循环平均分的位置
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
var today = new Date();
var month = today.getMonth() + 1 + "月";
var week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];
document.write(today.getFullYear() + "年" + month + today.getDate() + "日" + " " + week[today.getDay()]);
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scoreArr = scoreStr.split(";");
var average=0;
var num=0;
for (var i = 0; i < scoreArr.length; i++) {
var curNum = scoreArr[i].split(':') [1];
num += parseInt(curNum);
}
average = num / scoreArr.length;
document.write("--班级总分为:" + num);
document.write("--班级总平均分:" + average);
</script>
</head>
<body>
</body>
</html>
var scoreArr = scoreStr.split(";");
var average;
var num;
for (var i=0;i<scoreArr.length;i++){
var index = scoreArr[i].indexOf(':')+1;
num += parseInt(scorArr[i].substr(index));
average = num/soreArr.length;
}
循环里面用的soreArr,你上面面定义的是scoreArr
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
var today = new Date();
var month = today.getMonth() + 1 +"月";
var week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];
document.write(today.getFullYear()+"年"+month+today.getDate()+"日"+" "+week[today.getDay()]);
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scoreArr = scoreStr.split(";");
var average;
var num=0;
for (var i=0;i<scoreArr.length;i++){
var index = scoreArr[i].indexOf(':')+1;
num += parseInt(scoreArr[i].substr(index));
}
average = num/scoreArr.length;
document.write("--班级总分为:"+average);
</script>
</head>
<body>
</body>
</html>
额 ,我发现for循环里面scoreArr拼错两个,num要初始化,求平均分的代码位置不对,应该就酱
应该是num 没有初始化
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
var today = new Date();
var month = today.getMonth() + 1 +"月";
var week = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"];
document.write(today.getFullYear()+"年"+month+today.getDate()+"日"+" "+week[today.getDay()]);
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scoreArr = scoreStr.split(";");
var average;
var num = 0;
for (var i=0;i<scoreArr.length;i++){
var index = scoreArr[i].indexOf(':')+1;
num += parseInt(scoreArr[i].substr(index));
average = num/scoreArr.length;
}
document.write("--班级总分为:"+average);
</script>
</head>
<body>
</body>
</html>
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题