-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment5.js
131 lines (97 loc) · 2.82 KB
/
assignment5.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//the grades are in the order: Math, Science, English, SST
let studentGrades = {
};
//to add a new student with their grades
let addGrade=(studentName, grades)=> {
try {
studentGrades[studentName] = grades;
} catch (error) {
console.log(error.message)
}
}
addGrade("Elisha Bantana", [90, 70, 85, 67]);
addGrade("Sseruyombya Richard", [60, 50, 95, 57]);
addGrade("Matovu Edward", [45, 89, 45, 98]);
addGrade("Agnes Nakimuli", [70, 56, 79, 97]);
addGrade("Kasule Drrick", [40, 56, 30, 20])
//calculates the average grade for a specific learner
let calculateAverageGrade=(studentName)=> {
try {
let total = 0;
let average = 0;
for (let index = 0; index < studentGrades[studentName].length; index++) {
total += studentGrades[studentName][index];
}
average = total / (studentGrades[studentName].length);
console.log(`The average of ${studentName} is ${average}`);
} catch (error) { console.log(error.message) }
}
calculateAverageGrade("Sseruyombya Richard");
//find and display a specific student's name
let findStudentGrades=(studentName) =>{
try {
console.log(`The grades of ${studentName} are ${studentGrades[studentName]}`);
} catch (error) {
console.log(error.message);
}
}
findStudentGrades("Agnes Nakimuli");
//list all students and their grades
let listAllStudents=() =>{
try {
for (let [theirName, theirGrades] of Object.entries(studentGrades)) {
console.log(`${theirName}: ${theirGrades}`);
}
} catch (error) {
console.log(error.message);
}
}
listAllStudents();
//find best student based on total
let findOverallBestStudent=() =>{
try {
let personWithMaximum = null;
let total = 0;
let maximum = 0;
for (let [key, value] of Object.entries(studentGrades)) {
total = 0;
for (let index = 0; index < value.length; index++) {
total += value[index];
}
if (maximum < total) {
maximum = total;
personWithMaximum = key;
}
}
console.log(`The person with the highest total is ${personWithMaximum}, with ${maximum}`);
} catch (error) {
console.log(error.message);
}
}
findOverallBestStudent();
//sort students starting with the one with highest average to the least
let sortAccordingToAverage=()=> {
try {
let mean = 0;
let sum = 0;
let meanArray = [];
for (let [key, value] of Object.entries(studentGrades)) {
sum = 0;
for (let index = 0; index < value.length; index++) {
sum += value[index];
}
mean = sum / (value.length);
meanArray.push({ key, mean });
}
meanArray.sort(function (a, b) {
return b.mean - a.mean;
})
for (let person of meanArray) {
console.log(person);
}
} catch (error) {
console.log(error.message);
}
}
sortAccordingToAverage();
//failed to show non existing student