慕码人8075630
2021-11-25 13:05
<!DOCTYPE HTML>
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>系好安全带,准备启航</title>
<script type="text/javascript">
var myDate = new Date();
var myWeek = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
document.write(
myDate.getFullYear() + "年" +
(myDate.getMonth() + 1) + "月" +
myDate.getDate() + "日" + " " +
myWeek[myDate.getDay()] + "<br/>"
);
var sorceStr = "小明:87;小花:81;小红:97;小天:76;小张:74;小小:94;小西:90;小伍:76;小迪:64;小曼:76";
var sourceArr = sorceStr.split(";");
document.write("班级每人分数为:" + sourceArr + "<br/>");
var score = 0,
sum = 0,
num = sourceArr.length,
scoreArr = [];
for (var i = 0; i < sourceArr.length; i++) {
score = score + parseInt(sourceArr[i].slice(3,5));
scoreArr = scoreArr.concat(sourceArr[i].slice(3,5));
}
sum = Math.round(score / sourceArr.length);
function sortScore(a,b) {
return b - a;
}
var sortArr = scoreArr.sort(sortScore);
var higth = sortArr[0];
var low = sortArr[sortArr.length - 1];
document.write("班级分数由高到低为:" + sortArr + "<br/>");
document.write("班级总分为:" + score + "<br/>");
document.write("班级人数为:" + num + "<br/>");
document.write("班级人数最高分为:" + higth + "<br/>");
document.write("班级人数最低分为:" + low + "<br/>");
document.write("班级平均分为:" + 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"> window.onload = test; function test(){ // 定义一个数组存储学生成绩 var arr = [87,81,97,76,74,94,90,76,64,76]; // 计算数组的和和平均值 // 定义总和变量 var sum = 0; // 循环数组 for(var i = 0; i < arr.length; i++){ // 计算平均数 sum += arr[i]; } // 定义平均数 var avg = sum / arr.length; // 创建日期对象 var myDate = new Date(); // 获得年 var year = myDate.getFullYear(); // 获得月 var month = myDate.getMonth() + 1; // 获得日 var date = myDate.getDate(); // 获得星期几 var day = myDate.getDay(); // 获得小时 var hours = myDate.getHours(); // 获得分钟 var minutes = myDate.getMinutes(); // 获得秒数 var seconds = myDate.getSeconds(); // 向页面输出数据 document.write(year + "年" + month + "月" + date + "日 星期" + day + "<br />"); document.write("班级总分:" + sum + "<br/>"); document.write("班级平均分:" + Math.round(avg) + "<br/>"); } </script> </head> <body> </body> </html>
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题