<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>系好安全带,准备启航</title> <script type="text/javascript"> var mydate=new Date(); var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六",]; document.write(mydate.getFullYear()+"年"+parseFloat(mydate.getMonth()+1)+"月"+mydate.getDate()+"日 "+weekday[mydate.getDay()]+"<br>"); var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; var score1=new Array; var score2=new Array; var sum; score1=scoreStr.split(";"); for(var i=0;i<score1.length;i++){ document.write(score1[i]+"<br>") score2=score1[i].split(""); var a=score2[3]+score2[4]; document.write(a+"<br>") sum=sum+parseInt(a); } sum=Math.round(sum/10); document.write(sum); </script> </head> <body> </body> </html>
楼上正解,因为你只声明额sum,但是未设置初始值,默认为undefined,undefined/10是nan,math.floor后,还是nan.
给sum一个初始值0即可。