JS Lab 3
This commit is contained in:
@@ -14,20 +14,73 @@ export function getPassingStudents(students, passMark = 70) {
|
||||
return passingStudents;
|
||||
}
|
||||
|
||||
function calculateCourseAverages(students) {
|
||||
|
||||
export function calculateCourseAverages(students) {
|
||||
const groupedCourses = students.reduce((accumulator, { course, grade }) => {
|
||||
if (!accumulator[course]) {
|
||||
accumulator[course] = { total: 0, count: 0 };
|
||||
}
|
||||
|
||||
accumulator[course].total += Number(grade);
|
||||
accumulator[course].count += 1;
|
||||
|
||||
return accumulator;
|
||||
}, {});
|
||||
|
||||
return Object.entries(groupedCourses).reduce((accumulator, [course, values]) => {
|
||||
accumulator[course] = values.count === 0 ? 0 : values.total / values.count;
|
||||
return accumulator;
|
||||
}, {});
|
||||
}
|
||||
|
||||
function getTopPerCourse(students) {
|
||||
export function getTopPerCourse(students) {
|
||||
const topStudentsByCourse = students.reduce((accumulator, student) => {
|
||||
const currentTopStudent = accumulator[student.course];
|
||||
|
||||
if (!currentTopStudent || student.grade > currentTopStudent.grade) {
|
||||
accumulator[student.course] = student;
|
||||
}
|
||||
|
||||
return accumulator;
|
||||
}, {});
|
||||
|
||||
return Object.values(topStudentsByCourse);
|
||||
}
|
||||
|
||||
function calculateOverallStatistics(students) {
|
||||
const summary = document.getElementById("summary");
|
||||
summary.innerHTML += `<p>Total Students: ${students.length}</p><br>
|
||||
<p>Average Grade: </p><br>
|
||||
<p>Highest Grade: </p><br>
|
||||
<p>Lowest Grade: </p><br>
|
||||
<p>Average Attendance: %</p><br>
|
||||
<p>Unique Courses: </p><br>`;
|
||||
export function calculateOverallStatistics(students) {
|
||||
if (students.length === 0) {
|
||||
return {
|
||||
totalStudents: 0,
|
||||
averageGrade: 0,
|
||||
highestGrade: 0,
|
||||
lowestGrade: 0,
|
||||
averageAttendance: 0
|
||||
};
|
||||
}
|
||||
|
||||
const totals = students.reduce((accumulator, { grade, attendance }) => {
|
||||
const numericGrade = Number(grade);
|
||||
const numericAttendance = Number(attendance);
|
||||
|
||||
accumulator.totalStudents += 1;
|
||||
accumulator.gradeTotal += numericGrade;
|
||||
accumulator.attendanceTotal += numericAttendance;
|
||||
accumulator.highestGrade = Math.max(accumulator.highestGrade, numericGrade);
|
||||
accumulator.lowestGrade = Math.min(accumulator.lowestGrade, numericGrade);
|
||||
|
||||
return accumulator;
|
||||
}, {
|
||||
totalStudents: 0,
|
||||
gradeTotal: 0,
|
||||
attendanceTotal: 0,
|
||||
highestGrade: -Infinity,
|
||||
lowestGrade: Infinity
|
||||
});
|
||||
|
||||
return {
|
||||
totalStudents: totals.totalStudents,
|
||||
averageGrade: totals.gradeTotal / totals.totalStudents,
|
||||
highestGrade: totals.highestGrade,
|
||||
lowestGrade: totals.lowestGrade,
|
||||
averageAttendance: totals.attendanceTotal / totals.totalStudents
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user