慕雪1536570
2019-04-11 10:41
为什么运行没有结果显示
小哥哥,for循环错了。i=0;那你只能算到length-1的那一项,结果你还要算下标为length那一项,这一项是undefined的,所报错,另外你sum求和也错了
切割那里错了 单词是split。和上面说的一样,第一个document getDay那里少了个() 。for循环那里 arr.length就可以了,不用+1 因为是从i是从0开始的。最后 for循环里面的累加你重新搞一下吧,不用那么复杂。。看着眼花。
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
var week=["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
var mydate=new Date();
document.write(mydate.getFullYear()+"年"+(mydate.getMonth()+1)+"月"+mydate.getDate()+"日"+" "+week[mydate.getDay()]+"--班级总分为:");
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var str=scoreStr.split(";");
var sum=0;
for(var i=0;i<str.length;i++){
sum+=parseInt(str[i].substring(3,5));
}
document.write(Math.floor(sum/10));
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
week = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
];
var mydate = new Date();
document.write(mydate.getFullYear()+"年"+mydate.getMonth()+"月"+mydate.getDay()+"日"+" " + week[mydate.getDay()]+"--");
var scoreStr =
"小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var myarr = scoreStr.split(";");
var sum = 0;
for (var i = 0; i < myarr.length; i++) {
sum = sum + parseInt(myarr[i].slice(3));
}
document.write("班级总分为:" + sum+"<br />");
document.write("班级平均分为:" + Math.round(sum/myarr.length)+"<br />");
</script>
</head>
<body></body>
</html>
<script type="text/javascript">
var mydate =new Date();
document.write("今天是"+" "+mydate.getFullYear()+"年"+(mydate.getMonth()+1)+"月"+mydate.getDate()+"日");
//通过javascript的日期对象来得到当前的日期,并输出。
var weekday = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var mynum = mydate.getDay();
document.write(" "+weekday[mynum]+"<br>");
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
scoreStr = scoreStr.split(';');
var score =[
scoreStr[0].slice(3,5),
scoreStr[1].slice(3,5),
scoreStr[2].slice(3,5),
scoreStr[3].slice(3,5),
scoreStr[4].slice(3,5),
scoreStr[5].slice(3,5),
scoreStr[6].slice(3,5),
scoreStr[7].slice(3,5),
scoreStr[8].slice(3,5),
scoreStr[9].slice(3,5)
];
console.log(score);
var sum = 0;
for( var i =0; i < score.length;i++){
sum += parseInt(score[i]);
}
console.log(sum);
var avg = sum / score.length;
document.write("该班的平均分为:"+avg);
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
//从数组中将成绩撮出来,然后求和取整,并输出。
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
//通过javascript的日期对象来得到当前的日期,并输出。
var yihzou = ['星期天','星期一','星期二','星期三','星期四','星期五','星期六'];
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth()+1;
var day = time.getDate();
var xingqi = time.getDay();
var fenge = scoreStr.split(';');
var sum = 0;
for (var i = 0; i <= fenge.length-1; i++) {
var j = parseInt(fenge[i].substr(3));
sum = sum+j;
}
document.write(year+'年'+month+'月'+day);
document.write(yihzou[xingqi]+'--班级总分为:'+sum);
</script>
</head>
<body>
</body>
</html>
第一个document最后那里getDay();你少写了().....
哥这个我爱莫能助呀 弟弟非常想帮你 等我知识存储扎实了再来帮你
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题