86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
import {students} from "./data.js"
|
|
let passingStudents = [];
|
|
|
|
export function getPassingStudents(students, passMark = 70) {
|
|
if (isNaN(passMark) || passMark < 0) {
|
|
return false;
|
|
}
|
|
passingStudents = students.filter(student => {
|
|
let grade = parseInt(student.grade);
|
|
if (isNaN(grade)) return false;
|
|
|
|
return grade >= passMark;
|
|
});
|
|
return passingStudents;
|
|
}
|
|
|
|
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;
|
|
}, {});
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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
|
|
};
|
|
} |