forked from TrainingByPackt/Professional-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivity09.js
More file actions
60 lines (56 loc) · 1.69 KB
/
Activity09.js
File metadata and controls
60 lines (56 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class School {
constructor() {
this.students = [];
}
addStudent(student) {
this.students.push(student);
}
findByGrade(gradeLevel) {
return this.students.filter((s) => s.gradeLevel === gradeLevel);
}
findByAge(age) {
return this.students.filter((s) => s.age === age);
}
findByName(name) {
return this.students.filter((s) => s.name === name);
}
}
class Student {
constructor(name, age, gradeLevel) {
this.name = name;
this.age = age;
this.gradeLevel = gradeLevel;
this.courses = [];
}
calculateAverageGrade() {
const totalGrades = this.courses.reduce((prev, curr) => prev + curr.grade, 0);
return (totalGrades / this.courses.length).toFixed(2);
}
assignGrade(name, grade) {
this.courses.push(new Course(name, grade))
}
}
class Course {
constructor(name, grade) {
this.name = name;
this.grade = grade;
}
}
function test() {
const testSchool = new School();
const testStudent = new Student('Miku', 16, 10);
testStudent.assignGrade('music', 90);
testStudent.assignGrade('math', 80);
assert(testStudent.calculateAverageGrade() == 85.00);
const testStudent2 = new Student('Rin', 14, 8);
const testStudent3 = new Student('Len', 14, 8);
testSchool.addStudent(testStudent);
testSchool.addStudent(testStudent2);
testSchool.addStudent(testStudent3);
assert(testSchool.findByAge(14)[0].name === 'Rin');
assert(testSchool.findByGrade(10)[0].name === 'Miku');
assert(testSchool.findByGrade(8).length === 2);
assert(testSchool.findByName('Rin')[0].name === 'Rin');
console.log('TEST PASSED');
}
test();