Skip to content

Instantly share code, notes, and snippets.

@zt4ff
Created March 14, 2022 21:45
Show Gist options
  • Save zt4ff/7fb6fd44b29f2d2fdd92b434dc22f75e to your computer and use it in GitHub Desktop.
Save zt4ff/7fb6fd44b29f2d2fdd92b434dc22f75e to your computer and use it in GitHub Desktop.
// index.js
let counter = 0;
const generateId = () => {
++counter;
return counter;
};
class StudentManagement {
#students = [];
constructor(students) {
this.#students = students.map((student) => {
return { id: generateId(), name: student, performance: null };
});
}
add(...students) {
students.forEach((student) => {
this.#students.push({
id: generateId(),
name: student,
performance: null,
});
});
}
getStudent(studentIDOrName) {
if (studentIDOrName) {
return this.#students.filter(
(student) =>
studentIDOrName === student.id || studentIDOrName === student.name
);
}
return this.#students;
}
score(id, performance) {
if (performance >= 0 && performance <= 100) {
this.#students.find((student) => {
if (student.id === id) {
student.performance = performance;
}
});
} else throw new Error("Score should be between 0 and 100");
}
}
module.exports = StudentManagement;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment