Evan Burbidge
Aug 14, 2024

Did a staff really write this?

Let's look at the amount of loops, we can get passed / failed students in one reduce method, this reduce method could also process the student details instead of mapping them then mapping them again.... this is just.... a lot of loops

```

// Filter passed and failed students and process them

const { passedStudents, failedStudents, allDetails } = students.reduce<Record<string, ProcessedStudentDetails[]>>((acc, student) => {

acc = {

...acc,

allDetails: [...acc.allDetails, processStudentDetail(student)]

}

if (student.results.some(result => passingGrade(result.grade))) {

acc = {

...acc,

passedStudents: [...acc.passedStudents, processStudentDetail(student)]

}

}

if (student.results.every(result => !passingGrade(result.grade))) {

acc = {

...acc,

failedStudents: [...acc.failedStudents, processStudentDetail(student)]

}

}

return acc;

}, { passedStudents: [], failedStudents:[], allDetails: []})

```

Testing this in the console the one reduce method has a run time of 0.111 ms and the original code runs for 0.302

Evan Burbidge
Evan Burbidge

Written by Evan Burbidge

Javascript developer specialising in React, Vue and NodeJS. I like SQL and NoSql depends on the context really!

Responses (1)