<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h3>千峰H5成绩计算表</h3>
<table>
<tr>
<td>班级名称:</td>
<td><input type="text" id="bname" /></td>
<td></td>
</tr>
<tr>
<td>请输入班级人数:</td>
<td><input type="text" id="num" /></td>
<td></td>
</tr>
<tr>
<td>班级总成绩:</td>
<td><input type="text" id="sum" /></td>
<td><input type="button" value="提交" id="btn" /></td>
</tr>
<tr>
<td>最高成绩:</td>
<td><input type="text" id="max" /></td>
<td></td>
</tr>
<tr>
<td>最低成绩:</td>
<td><input type="text" id="min" /></td>
<td></td>
</tr>
<tr>
<td>平均成绩:</td>
<td><input type="text" id="avg" /></td>
<td></td>
</tr>
<tr>
<td>成绩高低排序:</td>
<td><textarea name="" id="sortScore" cols="30" rows="10"></textarea></td>
<td></td>
</tr>
</table>
</body>
</html>
<script type="text/javascript">
var btn = document.getElementById("btn");
var sum = document.getElementById("sum");
var max = document.getElementById("max");
var min = document.getElementById("min");
var avg = document.getElementById("avg");
var num = document.getElementById("num");
var sortScore = document.getElementById("sortScore");
btn.onclick = function() {
//1获取班级人数
var num1 = document.getElementById("num").value;
//var scores = [];
//var sum = 0;
//2根据人数输入每个人的成绩
var sum1 = 0;
var arr = [];
for (var i = 0; i < num1; i++) {
//3根据人数输入成绩
var score = parseInt(prompt("输入成绩"));
//4把成绩保存在数组中
arr[arr.length] = score;
sum1 += score;
}
// var sum = eval(num.join("+"));
sum.value = sum1;
var max1 = arr[0];
var min1 = arr[0];
for (var j = 0; j < arr.length; j++) {
if (arr[j] > max1) {
max1 = arr[j];
}
if (arr[j] < min1) {
min1 = arr[j];
}
}
max.value = max1;
min.value = min1;
avg.value = (sum1 / num1).toFixed(2);
var arr = arr.sort(function(a, b) {
return b - a;
})
sortScore.value = arr;
}
//var arr = [3,4,7];
//var sum = eval(arr.join("+"));
//alert(sum)
//eval():用来作字符串解析的方法,可以把一个字符串解析为js可以识别的代码来执行
//gloable
//var str = "alert(1)";
/*var str = "3+4";
alert(eval(str));*/
</script>