I'm trying retrieve the oldest person from the array using the .reduce() method in javascript. The array is below. What I'm struggling with is writing the correct syntax for the .reduce method to accomplish this. The code I'm using is below and I keep getting the result 'undefined'. I've been working with the code for a little while and feel I might be making a simple mistake but a nudge in the right direction would be helpful!
let findTheOldest = function(people) {
let total = 0
people.reduce((prev, curr) => {
if ((curr.yearOfDeath - curr.yearOfBirth) > total) {
total = (curr.yearOfDeath - curr.yearOfBirth);
}
return prev;
}, 0);
}
console.log(findTheOldest(people));
const people = [
{
name: 'Carly',
yearOfBirth: 1942,
yearOfDeath: 1970,
},
{
name: 'Ray',
yearOfBirth: 1962,
yearOfDeath: 2011
},
{
name: 'Jane',
yearOfBirth: 1912,
yearOfDeath: 1941
},
]
Please login or Register to submit your answer