ror 循環(huán)后面的document.write內(nèi)容無(wú)法顯示嗎
<!DOCTYPE??HTML> <html?> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"?/> <title>系好安全帶,準(zhǔn)備啟航</title> <script?type="text/javascript"> var?myd=new?Date(); document.write(myd+"<br>"); var?myday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]; document.write("今天是"+myday[myd.getDay()]+"<br>"); ??//通過(guò)javascript的日期對(duì)象來(lái)得到當(dāng)前的日期,并輸出。 ??//成績(jī)是一長(zhǎng)竄的字符串不好處理,找規(guī)律后分割放到數(shù)組里更好操作哦 ??var?scoreStr?=?"小明:87;小花:81;小紅:97;小天:76;小張:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; ??var?STR=scoreStr.split(";"); ?document.write(STR+"<br>"); document.write("學(xué)生"+"<br>"); ?var?score=new?Array(); ?var?sum=0; for(i=0;i<=STR.length;i++) { score[i]=parseInt(STR[i].substring(STR[i].indexOf(":")+1)); sum+=score[i]; document.write(Math.round(sum/score.length)+"<br>")}; document.write("學(xué)生"+"<br>"); document.write("123"); ??//從數(shù)組中將成績(jī)撮出來(lái),然后求和取整,并輸出。 </script> </head> <body> </body> </html>
輸出結(jié)果:
Thu Feb 23 2017 13:14:07 GMT+0800 (中國(guó)標(biāo)準(zhǔn)時(shí)間)
今天是星期四
小明:87,小花:81,小紅:97,小天:76,小張:74,小小:94,小西:90,小伍:76,小迪:64,小曼:76
學(xué)生
87
84
88
85
83
85
86
84
82
82
為什么for循環(huán)后面的document.write()語(yǔ)句里面的內(nèi)容都顯示不出來(lái)
2017-02-23
for(i in STR){
score[i]=STR[i].substring(STR[i].indexOf(":")+1);
var num = parseInt(score[i])
sum+=num;
};
document.write(Math.round(sum/score.length)+"<br>")
document.write("學(xué)生"+"<br>");
document.write("123");
直接給score[i]賦值是不正確的寫法,我給你改過(guò)來(lái)了,另外for循環(huán)里不要寫輸出,否則會(huì)輸出全部的計(jì)算結(jié)果,我們只是想要最終總和,寫在外面。
2017-02-23
1:for循環(huán) i<STR.length。因?yàn)閿?shù)組的下標(biāo)是從0開(kāi)始的,如果數(shù)組array=[1,2,3,4,5],ayyay.length就是5,遍歷數(shù)組如果從0開(kāi)始,遍歷到4就可以了。
2:document.write(Math.round(sum/score.length)+"<br>")把這句放在for循環(huán)外面。