-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB)assignment13.js
More file actions
40 lines (36 loc) · 1000 Bytes
/
B)assignment13.js
File metadata and controls
40 lines (36 loc) · 1000 Bytes
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 students = [
{ name: "John", marks: "92" },
{ name: "Oliver", marks: "85" },
{ name: "Michael", marks: "79" },
{ name: "Dwight", marks: "95"},
{ name: "Oscar", marks: "64" },
{ name: "Kevin", marks: "48" },
];
// a) Calculate grades on basis of marks
function calculateGrade(marks) {
if (marks > 90) {
return "A";
} else if (marks > 80) {
return "B";
} else if (marks > 70) {
return "C";
} else if (marks > 60) {
return "D";
} else if (marks > 50) {
return "E";
} else {
return "F";
}
}
// b) Map the grades of each student
let grades = students.map(student => ({name: student.name, grade: calculateGrade(student.marks)}));
// c) Group students according to the grades they have received and display
let grouped = {};
grades.forEach(student => {
if (!grouped[student.grade]) {
grouped[student.grade] = [student];
} else {
grouped[student.grade].push(student);
}
});
console.log(grouped);