Skip to content

Commit

Permalink
Create getAllStudentGrades.js
Browse files Browse the repository at this point in the history
  • Loading branch information
hattiza authored Jun 14, 2024
1 parent 8952aa9 commit d65f8c7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions getAllStudentGrades.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
let grades = [
{ student: "a", grade: 90 },
{ student: "b", grade: 80 },
{ student: "a", grade: 95 },
{ student: "a", grade: 91 },
];

function getAllStudentGrades(studentList, grades) {
let allStudentsGrades = [];
let student2grades = {};

// generate student2grades map
for (let i = 0; i < grades.length; i++) {
let student = grades[i].student;
let grade = grades[i].grade;

if (student2grades[student] === undefined) {
student2grades[student] = [grade];
} else {
let currentStudentGradesLength = student2grades[student].length;
student2grades[student][currentStudentGradesLength] = grade;
}
}

// reformat map to desired output
// [ { student: "a", grade: [90, 95, 91] }, { student: "b", grade: [80] } ]

for (let i = 0; i < studentList.length; i++) {
let student = studentList[i];
let grades = student2grades[student];
allStudentsGrades[allStudentsGrades.length] = {
student: student,
grade: grades,
};
}

return allStudentsGrades;
}

console.log(getAllStudentGrades(["a", "b"], grades)); // ==> [ { student: "a", grade: [90, 95, 91] }, { student: "b", grade: [80] } ]

0 comments on commit d65f8c7

Please sign in to comment.