function getNoteMinMax() {
let min = null, max = null;
for (const item of students.map(s => ({
student: s,
average: (s.marks.reduce((a, b) => a + b, 0) / s.marks.length)
})) {
if (min === null || item.average < min.average) {
min = item;
}
if (max === null || item.average > max.average) {
max = item;
}
}
return {
min: min.student,
max: max.student
}
}
let print = s => console.log(s);
let stud = [
{ name: "One", marks: [8, 10, 7, 5, 4] },
{ name: "Two", marks: [5, 2, 8, 5, 3] },
{ name: "Three", marks: [6, 9, 3, 6, 7] },
{ name: "Fore", marks: [1, 7, 8, 4, 10] },
{ name: "Five", marks: [5, 8, 6, 9, 3] },
];
let st_min = {...stud[0]}, st_max = {...stud[0]};
let av_min = av_max = st_min.marks.reduce((a, c) => a + c, 0) / st_min.marks.length;
for(let ob of stud) {
let av = ob.marks.reduce((a, c) => a + c, 0) / ob.marks.length;
if(av_min > av) {
st_min = {...ob}; av_min = av;
}
if(av_max < av) {
st_max = {...ob}; av_max = av;
}
}
print(st_min);
print(st_max);
const students = [
{ name: 'One', marks: [8, 10, 7, 5, 4] },
{ name: 'Two', marks: [5, 2, 8, 5, 3] },
{ name: 'Three', marks: [6, 9, 3, 6, 7] },
{ name: 'Fore', marks: [1, 7, 8, 4, 10] },
{ name: 'Five', marks: [5, 8, 6, 9, 3] },
];
const min = { student: null, avg: Number.POSITIVE_INFINITY };
const max = { student: null, avg: Number.NEGATIVE_INFINITY };
students.forEach(student => {
const { marks } = student;
const avg = marks.reduce((acc, c) => acc + c) / marks.length;
if (avg > max.avg) {
max.avg = avg;
max.student = student;
} else if (avg < min.avg) {
min.avg = avg;
min.student = student;
}
});
console.log('Student with mininal average mark', min.student);
console.log('Student with best average mark', max.student);