Repository navigation
Expand file tree
/
Copy pathAssignment 3.js
More file actions
166 lines (148 loc) · 5.28 KB
/
Copy pathAssignment 3.js
File metadata and controls
166 lines (148 loc) · 5.28 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Assignment 3: Student Class and Summary
class Student {
// constructor to set the properties
constructor (name, age, department, scores){
this.name = name;
this.age = age;
this.department = department;
this.scores = scores;
}
}
// Method to calculate the average score
getAverage() {
const total = this.scores.reduce(function(sum, score){
return sum + score;
}, 0);
return total / this.scores.length;
}
// Method to get student's grade
getGrade() {
const average = this.getAverage();
if (average >= 70) {
return "A";
} else if (average >= 60) {
return "B";
} else if (average >= 50) {
return "C";
} else if (average >= 40) {
return "D";
} else {
return "F";
}
}
// Method to check if the student has passed
hasPassed() {
return this.getAverage() >= 50;
}
// Method to get the highest score
getHighestScore() {
return Math.max(...this.scores);
}
// Method to get the lowest score
getLowestScore() {
return Math.min(...this.scores);
}
// Method to create Student Objects
const students = [
new Student("Ada", 20, "Computer Science", [78, 85, 91]),
new Student("Bob", 22, "Mathematics", [65, 70, 75]),
new Student("Charlie", 21, "Physics", [55, 60, 65]),
new Student("David", 23, "Chemistry", [45, 50, 55]),
new Student("Eve", 20, "Biology", [35, 40, 45]),
];
// Method of using the map function to create a summary of students
const studentSummaries = students.map(function(student) {
return {
name: student.name,
average: student.getAverage(),
grade: student.getGrade(),
status: student.hasPassed() ? "Passed" : "Failed",
};
});
// Method of using filter to find high-performing student
const topStudents = students.find(function(student) {
return student.getAverage() >= 70;
});
// Method of using some and every to check if all students passed or if any student failed
const hasFailedStudents = students.some(function(student) {
return student.hasPassed() === false;
});
const allStudentsPassed = students.every(function(student) {
return student.getAverage() >= 50;
});
console.log('At least one student failed' [hasFailedStudents]);
console.log('All students passed' [allStudentsPassed]);
//Method of using sort to rank students
const rankedStudents =[...students];
rankedStudents.sort(function(a, b) {
return b.getAverage() - a.getAverage();
});
//Calculate class statistics
// Total no of students
const totalStutdents = students.length
// Class average using reduce ()
const totalAverage = students.reduce(function(sum, student) {
return sum + student.getAverage();
});
const classAverage = totalAverage / students.length;
//Highest and Lowest performing student
const highestPerformingStudent = students.reduce(function(highest, student) {
if (students.getAverage() > highest.getAverage()){
return student;
} else {
return highest;
}
});
const lowestPerformingStudent = students.reduce(function(lowest, student) {
if (students.getAverage() < lowest.getAverage()){
return student;
}
} else {
return lowest;
}
// Using destructuring
const {name, department, age} = studentFound;
//Array destructing
const [firstScore, secondScore] = studentFound.scores;
console.log('Student Information');
console.log('Student Name: ' + name);
console.log('Department: ' + department);
console.log('Age: ' + age);
console.log('First Score: ' + firstScore);
console.log('Second Score: ' + secondScore);
// Using Spread and Rest Operators
//Spread Operator
const copiedScores = [... students(0).scores];
console.log ("\nCopiedScores:", CopiedScores);
// Rest Operator
function calculateTotal (... scores){
return sum + score;
} 0;
return total
const total = calculateTotal (70,85,90,65);
console.log('Total of extra scores: '(total));
// Final Report
console.log("\n==== STUDENT MANAGEMENT SYSTEM ====");
if (studentFound){
console.log('Student:' + studentFound.name);
console.log('Department:' + studentFound.department);
console.log('Age:' + studentFound.age);
console.log('Average:' + studentFound.getAverage().toFixed(2));
console.log('Grade:' + studentFound.getGrade());
console.log('Status:' + studentFound.hasPassed()?"Passed": "Failed");
console.log('Highest Score:' + studentFound.getHighestScore);
console.log('Lowest Score:'+ studentFound.getLowestScore);
}
// Top Students
console.log("\n==== TOP STUDENTS ====")
rankedStudents.forEachfunction(student, index)
console.log(
[index + 1]. student.name - [student.getAverage().toFixed(2)]);
// Class Statistics
console.log("\n==== CLASS STATISTICS ====");
console.log('Total Students:' + totalStudents);
console.log('Class Average:' + classAverage.toFixed(2));
console.log('Highest Performing Student:' + highestPerformingStudent.name);
console.log('Lowest Performing Student:' + lowestPerformingStudent.name);
console.log('At least one student failed:' + hasFailedStudent);
console.log('All student passed:' + allStudentsPassed);