题目描述:
var data = [
{ StudentNumber: 2022003, Chinese: 86, Math: 64, English: 80, TotalScore: "", Comment: "" },
{ StudentNumber: 2022004, Chinese: 78, Math: 99, English: 91, TotalScore: "", Comment: "" },
{ StudentNumber: 2022005, Chinese: 107.5, Math: 97, English: 70, TotalScore: "", Comment: "" },
{ StudentNumber: 2022006, Chinese: 112, Math: 61, English: 92, TotalScore: "", Comment: "" },
{ StudentNumber: 2022007, Chinese: 101, Math: 79, English: 104, TotalScore: "", Comment: "" },
{ StudentNumber: 2022008, Chinese: 71, Math: 72, English: 105, TotalScore: "", Comment: "" },
{ StudentNumber: 2022009, Chinese: 56, Math: 68, English: 61, TotalScore: "", Comment: "" },
{ StudentNumber: 2022010, Chinese: 98, Math: 83, English: 77, TotalScore: "", Comment: "" }
];
-
以上是某班级一次考试的成绩表。请计算每个学生总成绩,并按总成绩排名。统计各单科成绩第一名,输出其成绩与学号。
-
并根据总成绩计算每个学生成绩备注保存在 Comment 中,60分以上为D,70分以上为C,80分以上为B,90分以上为A,60分以下为E
-
代码详解在代码注释里面
<script>
var data = [{
StudentNumber: 2022003,
Chinese: 86,
Math: 64,
English: 80,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022004,
Chinese: 78,
Math: 99,
English: 91,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022005,
Chinese: 107.5,
Math: 97,
English: 70,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022006,
Chinese: 112,
Math: 61,
English: 92,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022007,
Chinese: 101,
Math: 79,
English: 104,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022008,
Chinese: 71,
Math: 72,
English: 105,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022009,
Chinese: 56,
Math: 68,
English: 61,
TotalScore: "",
Comment: ""
},
{
StudentNumber: 2022010,
Chinese: 98,
Math: 83,
English: 77,
TotalScore: "",
Comment: ""
}
];
// 构造函数
function ScoreHandling(data) {
this.data = data;
}
//根据成绩进行排名。可以是总成绩,语文成绩,数学成绩和英语。根据传入的参数不同排名不同
ScoreHandling.prototype.scoreSort=function(data,subject,subjectText){
// 用sort方法来排名
data.sort(function(a,b){
return b[subject]-a[subject];//根据上面传入的科目不同进行不同的排序用[]来代替.chinese等
})
//由于本次sort排名是由大到小,所以下标为0是本次排名科目的最高成绩的学生。
document.write(subjectText+'第一名是:'+data[0].StudentNumber+';成绩值为:'+data[0][subject]);
document.write('<br>');
}
// 函数原型 由于成绩的总成绩计算,平均分计算,等级划分都是一样的,所以放在原型里面
ScoreHandling.prototype.outputScore = function () {
let _data = this.data;//因为后面都要用,把成绩保存一下
for (let stu of _data) {
stu.TotalScore = stu.Chinese + stu.Math + stu.English;//计算总成绩
var stuscore = (stu.ToralScore / 3).toFixed(1);//计算平均成绩保留一位小数
//按照平均成绩划分等级
if (stuscore >= 110) {
stu.Comment = "A";
} else if (stuscore >= 100) {
stu.Comment = "B";
} else if (stuscore >= 90) {
stu.Comment = "C";
} else if (stuscore >= 72) {
stu.Comment = "D";
} else {
stu.Comment = "E";
}
}
//打印一下后的成绩
console.log(_data);
this.scoreSort(_data, 'TotalScore', '总成绩');
this.scoreSort(_data, 'Chinese', '语文');
this.scoreSort(_data, 'Math', '数学');
this.scoreSort(_data, 'English', '英语');
}
var score01=new ScoreHandling(data);
score01.outputScore();
</script>
代码运行结果
这是html页面的显示。
下面是打印框控制台的结果,展开了两个,后面的和前面的类似