I have an array of objects that contains the name and marks of students. like below
How can I calculate the 'average' marks each student has and compare the 'average' marks to get the top student. I don’t want to use ES6
var Students = [{
name: "Bob",
marks: [78, 80, 89, 90, 68]
},
{
name: "Alin",
marks: [87, 60, 59, 70, 68]
},
{
name: "bikash",
marks: [82, 60, 79, 60, 80]
}
];
var average;
var newArray = [];
for (let i = 0; i < Students.length; i++) {
var marks = Students[i]["marks"];
var total = 0;
console.log(marks);
for (var j = 0; j < marks.length; j++) {
total += marks[j];
}
average = total / marks.length;
newArray.push(average)
var msg = Students[i]["name"] + " has average mark: " + average;
console.log(msg)
}
console.log(newArray)
I want to insert the average numbers in an object along with the names like [{name: "Bob", average:89.4},{name: "Alin", average:87.2},{name: "Bikash", average:89.4} ] and sort the object.
Finally, I want to have console.log as Bob is the Top student OR Alin and Bob are the brightest students (in case both of them have the same average number)