Last active
December 9, 2018 10:30
-
-
Save ronit-mukherjee/e69d352f5be2a3559ddd7db1af5cc895 to your computer and use it in GitHub Desktop.
Solution for Program #RMJPQ3 given on http://ronitmukherjee.com/practice-javascript-programs-for-interview/#RMJPQ4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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