Mini-Build: Student Grade Manager
Build a Student Grade Manager using objects, classes, methods, and array operations. This project combines all concepts from this module — object-oriented design with practical data management.
Grade Manager Features
- Student class — name, ID, grades array
- Add grades — push grades to student's array
- Calculate average — reduce to sum, divide by length
- Get highest/lowest — Math.max/min with spread
- Class ranking — sort students by average
- JSON export — serialize data for storage
Student Manager Code
class Student {
constructor(name, id) {
this.name = name;
this.id = id;
this.grades = [];
}
addGrade(subject, score) {
this.grades.push({ subject, score });
}
get average() {
if (this.grades.length === 0) return 0;
const sum = this.grades.reduce((acc, g) => acc + g.score, 0);
return Math.round(sum / this.grades.length * 10) / 10;
}
get highest() {
return Math.max(...this.grades.map(g => g.score));
}
get letterGrade() {
const avg = this.average;
if (avg >= 90) return "A";
if (avg >= 80) return "B";
if (avg >= 70) return "C";
if (avg >= 60) return "D";
return "F";
}
toString() {
return `${this.name} (ID: ${this.id}) — Avg: ${this.average} (${this.letterGrade})`;
}
}
// Create students
const students = [
new Student("Alice", "S001"),
new Student("Bob", "S002"),
new Student("Charlie", "S003"),
];
// Add grades
students[0].addGrade("Math", 95);
students[0].addGrade("Science", 88);
students[0].addGrade("English", 92);
students[1].addGrade("Math", 78);
students[1].addGrade("Science", 82);
students[1].addGrade("English", 74);
students[2].addGrade("Math", 88);
students[2].addGrade("Science", 91);
students[2].addGrade("English", 85);
// Display all students
console.log("📊 Student Report:");
students.forEach(s => console.log(` ${s.toString()}`));
// Ranking by average
const ranked = [...students].sort((a, b) => b.average - a.average);
console.log("\n🏆 Class Ranking:");
ranked.forEach((s, i) => console.log(` #${i + 1}: ${s.name} — ${s.average}`));
// Export as JSON
console.log("\n📄 JSON Export:");
console.log(JSON.stringify(students, null, 2));Tip
Tip
Make methods return 'this' to enable method chaining: student.addGrade('Math', 95).addGrade('Science', 88). This fluent API pattern is used in jQuery, D3.js, and many builder patterns.
keys/values/entries return arrays for .map/.filter.
Common Mistake
Warning
Using Math.max() on an empty array with spread returns -Infinity: Math.max(...[]) === -Infinity. Always check array length first or provide a fallback: grades.length > 0 ? Math.max(...grades) : 0.
Practice Task
Note
Extend the Grade Manager: (1) Add a getSubjectAverage(subject) method. (2) Add a static method to find the class topper. (3) Add a method that returns passing/failing status (average >= 60). (4) Export and re-import the data with JSON.
Quick Quiz
Key Takeaways
- Build a Student Grade Manager using objects, classes, methods, and array operations.
- Student class — name, ID, grades array
- Add grades — push grades to student's array
- Calculate average — reduce to sum, divide by length