How can I further optimize following student average coding challenge?
Statement:- Given a list of N students, every student is marked for M subjects. Each student is denoted by an index value. Their teacher Ms. Margaret must ignore the marks of any 1 subject for every student. For this she decides to ignore the subject which has the lowest class average. Your task is to help her find that subject, calculate the total marks of each student in all the other subjects and then finally return the array of the total marks scored by each student.
Input Specification: input1: An integer value N denoting number of students
input2: An integer value M denoting number of subjects
input3: A 2-D integer array of size N'M containing the marks of all students in each subject.
Output Specification:
Return an integer array of size N containing the total marks of each student after deducting the score for that one subject.
const students = 3 const subjects = 5 const arr = [ [75, 76, 65, 87, 87], [78, 76, 68, 56, 89], [67, 87, 78, 77, 65] ] let map = arr.reduce((acc, list) => { list.forEach((mark, i) => { if(!acc[i]) { acc[i] = mark } else { acc[i] = acc[i] + mark } }); return acc; }, {}); let indexToIgnore = 0; let lowest = map[0] / students for (const i in map) { if(i !== '0') { if (Object.hasOwnProperty.call(map, i)) { const total = map[i]; const average = total / students if((average) < lowest) { indexToIgnore = Number(i) } } } } const result = arr.reduce((acc, list) => { const sum = list.reduce((_acc, no, i) => i !== indexToIgnore ? _acc + no: _acc ,0) acc.push(sum); return acc; }, []) console.log(result) // => [ 325, 299, 296 ]