<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系好安全带,准备启航</title> <script type="text/javascript"> //通过javascript的日期对象来得到当前的日期,并输出。 var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; var myDate = new Date(); var myYear = myDate.getFullYear(); var myMonth = myDate.getMonth() + 1; var day = myDate.getDate(); var week = weekday[myDate.getDay()]; //成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦 var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; var sum = 0; var av = 0 //var scoreArr = new Array(); var scoreArr1 = new Array(); scoreArr = scoreStr.split(';'); scoreArr = scoreArr.join(':'); scoreArr = scoreArr.split(':'); for (var i = 0 ; i < scoreArr.length/2 ; i ++){ scoreArr1 = scoreArr[2*i + 1]; sum += parseInt(scoreArr1[i]); } av = sum/scoreArr1.length; av = Math.floor(); document.write(myYear+"年"+myMonth+"月"+day+"日"+" "+week+"--班级总平均分为:"+av); //从数组中将成绩撮出来,然后求和取整,并输出。 </script> </head> <body> </body> </html>
为什么类型不对,总平均分显示NaN?
改过的地方给你添加注释了,自己看看 <!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系好安全带,准备启航</title> <script type="text/javascript"> //通过javascript的日期对象来得到当前的日期,并输出。 var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; var myDate = new Date(); var myYear = myDate.getFullYear(); var myMonth = myDate.getMonth() + 1; var day = myDate.getDate(); var week = weekday[myDate.getDay()]; //成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦 var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; var sum = 0; var av = 0 //var scoreArr = new Array(); var scoreArr1 = new Array(); scoreArr = scoreStr.split(';'); scoreArr = scoreArr.join(':'); scoreArr = scoreArr.split(':'); for (var i = 0 ; i < scoreArr.length/2 ; i ++){ scoreArr1[i] = scoreArr[2*i + 1]; //这里应该是数组元素赋值给数组元素而不是赋值给数组 sum += parseInt(scoreArr1[i]); } document.write(); av = sum/scoreArr1.length; av = Math.floor(av); // floor()里面要给参数,不然你是要取整谁啊。。。。 document.write(myYear+"年"+myMonth+"月"+day+"日"+" "+week+"--班级总平均分为:"+av); </script> </head> <body> </body> </html>
1、 scoreArr1 = scoreArr[2*i + 1];
scoreArr1 这里赋值,就是一个数字而已,不是数组。不会有length属性。
2、
av = sum/scoreArr1.length;
av = Math.floor();
使用不对。
Math.floor(averageScore);
还有NaN = Not a Number
额...你看就懂了。
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
//通过javascript的日期对象来得到当前的日期,并输出。
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var myDate = new Date();
var myYear = myDate.getFullYear();
var myMonth = myDate.getMonth() + 1;
var day = myDate.getDate();
var week = weekday[myDate.getDay()];
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var sum = 0;
var av = 0
//var scoreArr = new Array();
var scoreArr1 = new Array();
scoreArr = scoreStr.split(';');
scoreArr = scoreArr.join(':');
scoreArr = scoreArr.split(':');
var dd = 0;
for (var i = 0 ; i < scoreArr.length ; i ++){
if(!(i%2==0)){
dd++;
scoreArr1 = scoreArr[i];
sum += parseInt(scoreArr1);
}
}
av = sum/dd;
av = Math.floor(av);
document.write(myYear+"年"+myMonth+"月"+day+"日"+" "+week+"--班级总平均分为:"+av);
//从数组中将成绩撮出来,然后求和取整,并输出。
</script>
</head>
<body>
</body>
</html>