const getLeaderBoard = (students) => {
// do you magic
let leaderboard = students
.filter((stud) => {
let totalGrade = Object.values(stud.grades).reduce(
(acc, curr) => acc + curr
);
return totalGrade > 85;
})
.map((stud) => {
let totalGrade = Object.values(stud.grades).reduce(
(acc, curr) => acc + curr
);
return { ...stud, totalGrade };
})
.sort((a, b) => b.totalGrade - a.totalGrade);
return leaderboard;
};
const students = [
{
name: "3bkreem",
class: "3rd",
grades: {
math: 25,
physics: 25,
programming: 25,
english: 25,
},
},
{
name: "Samer",
class: "3rd",
grades: {
math: 10,
physics: 15,
programming: 12,
english: 19,
},
},
{
name: "Ahmed",
class: "3rd",
grades: {
math: 20,
physics: 20,
programming: 20,
english: 20,
},
},
{
name: "Ali",
class: "3rd",
grades: {
math: 20,
physics: 25,
programming: 19,
english: 22,
},
},
{
name: "Elgoker",
class: "3rd",
grades: {
math: 23,
physics: 25,
programming: 19,
english: 22,
},
},
];
console.log(getLeaderBoard(students));
Y29uc3QgZ2V0TGVhZGVyQm9hcmQgPSAoc3R1ZGVudHMpID0+IHsKICAvLyBkbyB5b3UgbWFnaWMKICBsZXQgbGVhZGVyYm9hcmQgPSBzdHVkZW50cwogICAgLmZpbHRlcigoc3R1ZCkgPT4gewogICAgICBsZXQgdG90YWxHcmFkZSA9IE9iamVjdC52YWx1ZXMoc3R1ZC5ncmFkZXMpLnJlZHVjZSgKICAgICAgICAoYWNjLCBjdXJyKSA9PiBhY2MgKyBjdXJyCiAgICAgICk7CiAgICAgIHJldHVybiB0b3RhbEdyYWRlID4gODU7CiAgICB9KQogICAgLm1hcCgoc3R1ZCkgPT4gewogICAgICBsZXQgdG90YWxHcmFkZSA9IE9iamVjdC52YWx1ZXMoc3R1ZC5ncmFkZXMpLnJlZHVjZSgKICAgICAgICAoYWNjLCBjdXJyKSA9PiBhY2MgKyBjdXJyCiAgICAgICk7CiAgICAgIHJldHVybiB7IC4uLnN0dWQsIHRvdGFsR3JhZGUgfTsKICAgIH0pCiAgICAuc29ydCgoYSwgYikgPT4gYi50b3RhbEdyYWRlIC0gYS50b3RhbEdyYWRlKTsKCiAgcmV0dXJuIGxlYWRlcmJvYXJkOwp9OwoKY29uc3Qgc3R1ZGVudHMgPSBbCiAgewogICAgbmFtZTogIjNia3JlZW0iLAogICAgY2xhc3M6ICIzcmQiLAogICAgZ3JhZGVzOiB7CiAgICAgIG1hdGg6IDI1LAogICAgICBwaHlzaWNzOiAyNSwKICAgICAgcHJvZ3JhbW1pbmc6IDI1LAogICAgICBlbmdsaXNoOiAyNSwKICAgIH0sCiAgfSwKCiAgewogICAgbmFtZTogIlNhbWVyIiwKICAgIGNsYXNzOiAiM3JkIiwKICAgIGdyYWRlczogewogICAgICBtYXRoOiAxMCwKICAgICAgcGh5c2ljczogMTUsCiAgICAgIHByb2dyYW1taW5nOiAxMiwKICAgICAgZW5nbGlzaDogMTksCiAgICB9LAogIH0sCgogIHsKICAgIG5hbWU6ICJBaG1lZCIsCiAgICBjbGFzczogIjNyZCIsCiAgICBncmFkZXM6IHsKICAgICAgbWF0aDogMjAsCiAgICAgIHBoeXNpY3M6IDIwLAogICAgICBwcm9ncmFtbWluZzogMjAsCiAgICAgIGVuZ2xpc2g6IDIwLAogICAgfSwKICB9LAoKICB7CiAgICBuYW1lOiAiQWxpIiwKICAgIGNsYXNzOiAiM3JkIiwKICAgIGdyYWRlczogewogICAgICBtYXRoOiAyMCwKICAgICAgcGh5c2ljczogMjUsCiAgICAgIHByb2dyYW1taW5nOiAxOSwKICAgICAgZW5nbGlzaDogMjIsCiAgICB9LAogIH0sCgogIHsKICAgIG5hbWU6ICJFbGdva2VyIiwKICAgIGNsYXNzOiAiM3JkIiwKICAgIGdyYWRlczogewogICAgICBtYXRoOiAyMywKICAgICAgcGh5c2ljczogMjUsCiAgICAgIHByb2dyYW1taW5nOiAxOSwKICAgICAgZW5nbGlzaDogMjIsCiAgICB9LAogIH0sCl07Cgpjb25zb2xlLmxvZyhnZXRMZWFkZXJCb2FyZChzdHVkZW50cykpOwo=