Simple Reduce Function - JavaScript

Viewed 68

I'm working with the reduce function on a super simple data set, and I'm getting NaN as the result when I didn't expect it:

let students = [{name: 'Leah', grade: 94},{name: 'Savannah', grade: 73},{name: 'Killian', grade: 38}];

let highest = students.reduce(
    (high, current) => Math.max(high.grade, current.grade)
    )

console.log(highest); // outputs NaN

The weird thing is, if I change the very first line to the following: (just taking out the third object)

let students = [{name: 'Leah', grade: 94},{name: 'Savannah', grade: 73}];

then this runs correctly and outputs 94.


Why does adding that one extra object cause problems?

2 Answers
Related