-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9.High performing students.js
More file actions
68 lines (55 loc) · 2.09 KB
/
9.High performing students.js
File metadata and controls
68 lines (55 loc) · 2.09 KB
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
/* You are given an array of objects representing a group of students, each with a name and an array of test scores.
Your task is to use map, filter, and reduce to calculate the average test score for each student, and then return an array of objects
containing only the students who have an average score above 90.
Example 1:
Input: students = [
{ name: "Alice", scores: [90, 85, 92] },
{ name: "Bob", scores: [75, 80, 85] },
{ name: "Charlie", scores: [90, 95, 85] },
{ name: "Jack", scores: [100, 100, 100] }
];
Output: [ { name: 'Jack', average: 100 } ]
Example 2:
Input: students = [
{ name: "Tiger", scores: [90, 85, 92] },
{ name: "Robin", scores: [75, 80, 85] },
{ name: "Charlie", scores: [90, 95, 95] },
{ name: "Jack", scores: [40, 70, 80] }
];
Output: [ { name: 'Charlie', average: 93 } ]
Example 3:
Input: students = [
{ name: "Alice", scores: [90, 95, 92] },
{ name: "Tiger", scores: [75, 80, 85] },
{ name: "Charlie", scores: [90, 95, 85] },
{ name: "Robin", scores: [90, 70, 100] }
];
Output: [ { name: 'Alice', average: 92 } ]
Example 4:
Input: students = [
{ name: "Robin", scores: [90, 95, 92] },
{ name: "Bob", scores: [75, 80, 85] },
{ name: "Charlie", scores: [80, 75, 85] },
{ name: "Jack", scores: [70, 80, 70] }
];
Output: [ { name: 'Robin', average: 92 } ]
*/
const getHighestStudentFunction = (students) => {
const studentsWithAverageScore = [];
students.map(student => {
let studentWithAverage = {name : "", average: 0}
studentWithAverage.name = student.name;
let average = student.scores.reduce((sum, score) => sum + score, 0) / student.scores.length;
studentWithAverage.average = Math.floor(average);
studentsWithAverageScore.push(studentWithAverage);
})
return studentsWithAverageScore.filter(student => student.average > 90 )
}
const students = [
{ name: "Robin", scores: [90, 95, 92] },
{ name: "Bob", scores: [75, 80, 85] },
{ name: "Charlie", scores: [80, 75, 85] },
{ name: "Jack", scores: [70, 80, 70] }
];
const result = getHighestStudentFunction(students);
console.log(result);