qq_慕勒5341377
2019-10-22 17:39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script type="text/javascript">
//通过javascript的日期对象来得到当前的日期,并输出。
var date = new Date();
var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
//成绩是一长窜的字符串不好处理,找规律后分割放到数组里更好操作哦
var newSt = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var newStr=newSt.split(";");
var sum=0;
for(var i=0;i<newStr.length;i++){
sum=sum+Number(newStr[i].substring(3,5));
}
//从数组中将成绩撮出来,然后求和取整,并输出。
document.write(date.toString().substring(11, 15) + "年" + date.getMonth() + "月" + date.toString().substring(8, 10) + "日" + " " + weekday[date.getDay()] + ",班级总分:"+sum);
</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">
function getCurrentDay() {
var date = new Date();
var week = date.getDay();
var dateStr = date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日";
switch (week) {
case 0:
dateStr += " 星期日";
break;
case 1:
dateStr += " 星期一";
break;
case 2:
dateStr += " 星期二";
break;
case 3:
dateStr += " 星期三";
break;
case 4:
dateStr += " 星期四";
break;
case 5:
dateStr += " 星期五";
break;
case 5:
dateStr += " 星期六";
break;
}
return dateStr;
}
var scoreStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var score = scoreStr.split(";");
var totalScore = 0;
for (item in score) {
if (score[item].indexOf(":") != -1) {
totalScore = totalScore + parseInt(score[item].substring(score[item].indexOf(":")+1));
}
}
document.write(getCurrentDay() + "--班级总分为"+totalScore);
</script>
</head>
<body>
</body>
</html>
这个适用性较差,如果成绩是三位数或四位数,这样做就不行
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题