<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>字符串综合应用</title> <script type="text/javascript"> var scoreStr = "小明:87;小花:801;小红:907;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; //split()分割字符串 var a = scoreStr.split(";"); document.write(a); var s = new Array(); var sum = 0; for(var i = 0; i < a.length; i++){ //substring()提取字符串中介于两个指定下标之间的字符; //indexOf()返回某个指定的字符串值在字符串中首次出现的位置; s[i] = parseInt(a[i].substring(a[i].indexOf(":") + 1)); sum += s[i]; } document.write("</br>"+s); document.write("</br>"+"平均数为:" + sum / a.length + "分"); </script> </head> <body> </body> </html>
本文首发于Calamus的博客。