Skip to content

Instantly share code, notes, and snippets.

@ronit-mukherjee
Last active December 9, 2018 10:30
Show Gist options
  • Save ronit-mukherjee/e69d352f5be2a3559ddd7db1af5cc895 to your computer and use it in GitHub Desktop.
Save ronit-mukherjee/e69d352f5be2a3559ddd7db1af5cc895 to your computer and use it in GitHub Desktop.
//https://jsfiddle.net/mukherjeeronit/906gx2cb/
function findTopper(students = []) {
var topper = {
percentage: 0
};
for (var i = 0; i < students.length; i++) {
var student = students[i];
var marks = Object.values(student.marks); //[66,80,97]
var totalMarks = marks.reduce(function(total, mark) {
return total + mark;
}, 0);
var percentage = (totalMarks / (marks.length * 100)) * 100;
if (percentage > topper.percentage) {
topper = student;
topper.percentage = percentage;
}
}
return topper;
}
var students = [{
name: "Sumit",
marks: {
english: 60,
maths: 80,
science: 97
}
},
{
name: "Amit",
marks: {
english: 65,
maths: 90,
science: 77
}
}
];
var topper = findTopper(students);
console.log(topper.name + " has topped the class with " + topper.percentage + "%");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment