噼里趴趴
2019-02-27 16:11
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array对象 </title>
<script type="text/javascript">
var time=new Date();
var a=time.getFullYear();
var b=time.getMonth()+1;
var c=time.getDate();
var d=time.getDay();
var f=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
document.write(a+"年"+b+"月"+c+"日"+" "+" "+" "+" "+f[d]+"<br/>");
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var add=scoreStr.split(";")
var sum=0
for(i=0;i<add.length;i++){
sum += parseInt ( add[i].slice(add[0].indexOf(":")+1));
}
document.write(sum/add.length+"<br/>")
</script>
</head>
<body>
</body>
</html>
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76"; var myArr = scoreStr.split(";"); var sum=0; for (var i=0;i<myArr.length;i++){ sum+=parseInt(myArr[i].split(":")[1],10) }
这种方法最简单,当然你那个也可以就是稍微复杂化了点!结果都相同。
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
//通过javascript的日期对象来得到当前的日期,并输出。
var date = new Date();
var a = date.getFullYear();
var b = date.getMonth();
var c = date.getDate();
var d = date.getDay();
var f = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
var str1 = a+"年"+b+"月"+c+"日"+" "+f[d]+"<br>";
document.write(str1);
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var arr1 = scoreStr.split(";");
var arr2 = new Array();
var sum = 0;
for(var i = 0;i<arr1.length;i++){
arr2[i]=arr1[i].split(":");
sum += parseInt(arr2[i][1]);
}
//从数组中将成绩撮出来,然后求和取整,并输出。
document.write("班级总分:"+parseInt(sum/arr1.length));
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
//通过javascript的日期对象来得到当前的日期,并输出。
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDate();
var weeks=new Array("星期天","星期一","星期二","星期三","星期四","星期五","星期六");
var week=date.getDay();
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var scores=scoreStr.split(";");
var sum=0;
for(var i=0;i<scores.length;i++){
var scoreString=scores[i].toString();
var temp=scoreString.split(":");
sum=sum+Number(temp[1]);
}
//从数组中将成绩撮出来,然后求和取整,并输出。
var average=parseInt(sum/scores.length);
document.write(year+"年"+month+"月"+day+"日"+" "+weeks[week]+"--班级总分为:"+average);
</script>
</head>
<body>
</body>
</html>
JavaScript进阶篇
468060 学习 · 21891 问题
相似问题