<body>
<script type="text/javascript">
class Student {
constructor(_name, _sid, _js, _ai, _hp, _wp, _ec) {
this.name=_name;
this.sid=_sid;
this.js=_js;
this.ai=_ai;
this.hp=_hp;
this.wp=_wp;
this.ec=_ec;
}
print(){
document.write(`${this.name} \t ${this.sid} \t ${this.js} \t ${this.ai} \t ${this.hp} \t ${this.wp} \t ${this.ec} \t ${this.avg}<br>`);
}
static printHead(){
document.write("name \t sid \t js \t vai \t hp \t wp \t ec \t avg<br>");
}
get avg(){
return Math.round((this.js+this.ai+this.hp+this.wp+this.ec)/5);
}
}
class StudentList{
constructor(){
this.students=[];
}
add(_name, _sid, _js, _ai, _hp, _wp, _ec){
var s=new Student(_name, _sid, _js, _ai, _hp, _wp, _ec);
//alert(_name)
this.students.push(s);
}
sort(){
for(var i=0; i<this.students.length-1; i++)
{
for(var j=i+1; j<this.students.length; j++)
{
var t;
if(this.students[i].avg<this.students[j].avg)
{
t=this.students[i];
this.students[i]=this.students[j];
this.students[j]=t;
}
}
}
document.write("data is sorted")
}
print(){
if(this.students.length==0)
document.write("no data!<br>");
document.write("<pre>");
Student.printHead();
for(var stu of this.students){
stu.print();
}
document.write("</pre>");
}
delete(sid){
var found=false;
var i=0;
while(!found && i<this.students.length)
{
if(this.students[i].sid==sid)
found=true;
else
i=i+1;
}
if(found)
{
this.students.splice(i,1);
return true;
}
else {
document.write("not found!<br>");
return false;
}
}
}
var sname=["aaaa","bbbb","cccc","dddd","eeee",
"ffff","gggg","hhhh","iiii","jjjj"];
var sid=["a001","a002","a003","a004","a005",
"a006","a007","a008","a009","a010",]
var scores=[[60,50,80,90,30],[78,65,86,99,55],[46,68,64,80,90],[67,87,90,30,20],[65,76,88,68,57],
[64,56,66,98,79],[66,80,74,78,87],[54,56,65,97,96],[76,78,88,39,98],[54,60,66,67,89]];
var stdList=new StudentList();
for(var i=0; i<10; i++)
{
stdList.add(sname[i], sid[i], scores[i][0], scores[i][1], scores[i][2], scores[i][3], scores[i][4]);
}
stdList.print();
stdList.sort();
stdList.print();
if(stdList.delete("a003"))
stdList.print();
</script>