-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAllStudentGrades.js
40 lines (33 loc) · 1.14 KB
/
getAllStudentGrades.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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] } ]