问答详情
源自:7-4 返回星期方法

为什么结果是NaN

<!DOCTYPE  HTML>

<html >

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>系好安全带,准备启航</title>


<script type="text/javascript">


var date=new Date() ;


 var year=date.getFullYear();


 var month=date.getMonth()+1;


 var day=date.getDate();


  var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];


  var week=date.getDay();


  document.write(year+"年"+month+"月"+day+"日"+" "+weekday[week])

 var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";

 

  var score=new Array();

  

  score1=scoreStr.split(';');


  var sum=0;



for(var i=0;i<score1.length;i++){


    



var score2=new Array();

    score2=score1[i].split(':');


    sum+=parseInt(score2[i]);


}


document.write("<br><br>总分数:"+sum);


document.write("<br><br>平均分:"+Math.round(sum/(score1.length)));


</script>

</head>

<body>

</body>

</html>


提问者:叛逆春 2015-08-03 11:34

个回答

  • 忆木偶
    2015-08-04 11:35:57

    错在这一步sum+=parseInt(score2[i]);

    因为score2=score1[i].split(':');获取出来的结果是score2=["小明", "87"],score2=["小花", "81"],获取不到具体的数值,所以把sum+=parseInt(score2[i]);改为sum+=parseInt(score2[1]);就可以了