1

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
      },
    ]
3
  • Note that the first argument in reduce is not the previous value but accumulator. The accumulator accumulates callback's return values.
    – kiranvj
    Apr 11, 2020 at 2:30
  • Are you dead-set on using reduce?
    – Joel Hager
    Apr 11, 2020 at 2:56
  • Hi Joel, if you have anything better in mind, please share. I have shared an answer which does not use reduce. You never know what new thing you can learn! Apr 11, 2020 at 3:07

4 Answers 4

3

You can use a comparator to sort the whole array in the order you want as well and get the first element. Ironically, the example provided in this link is almost the same as we have here :
http://www.mattmorgante.com/technology/javascript-sort-compare

Does not use reduce but thought this was interesting.

    const people = [{
        name: 'Carly',
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: 'Ray',
        yearOfBirth: 1962,
        yearOfDeath: 2011
      },
      {
        name: 'Jane',
        yearOfBirth: 1912,
        yearOfDeath: 1941
      },
    ]
    const oldestPeople = people.sort(function(a,b) {
  const lastPerson = a.yearOfDeath - a.yearOfBirth;
  const nextPerson = b.yearOfDeath - b.yearOfBirth;
  if (lastPerson > nextPerson) {
    return -1;
  } else {
    return 1;
  }
});

console.log(people[0]);

1

You have undefined because the function findTheOldest just return nothing.
If you want to keep your function and using your variable total (not very optimised) you need to return the oldestPerson like this:

let findTheOldest = function(people) {
  let total = 0
  let OldestPerson = {}
  people.reduce((prev, curr) => {
    if ((curr.yearOfDeath - curr.yearOfBirth) > total) {
      total = (curr.yearOfDeath - curr.yearOfBirth);
      OldestPerson = curr
    }
    return prev;
  })
  return OldestPerson
}
1

For reduce to work, you'd have to either re-calculate the age of the accumulator every time (somewhat inelegant), or save it in an outside variable. Then, return inside the callback depending on whether the accumulator or the current item being iterated over has the greater age:

const getAge = ({ yearOfBirth, yearOfDeath }) => yearOfDeath - yearOfBirth;
const findTheOldest = function(people) {
  let accumAge = getAge(people[0]);
  return people.reduce((accum, curr) => {
    const currAge = getAge(curr);
    if (currAge > accumAge) {
      accumAge = currAge;
      return curr;
    }
    return accum;
  });
}

const people = [{
    name: 'Carly',
    yearOfBirth: 1942,
    yearOfDeath: 1970,
  },
  {
    name: 'Ray',
    yearOfBirth: 1962,
    yearOfDeath: 2011
  },
  {
    name: 'Jane',
    yearOfBirth: 1912,
    yearOfDeath: 1941
  },
]
console.log(findTheOldest(people));

While it's possible with reduce, I think it would be somewhat ugly and not entirely appropriate

1

You can use reduce. While it is ugly it shortens the code quite a bit =)

const people = [
      {
        name: 'Carly',
        yearOfBirth: 1942,
        yearOfDeath: 1970,
      },
      {
        name: 'Ray',
        yearOfBirth: 1962,
        yearOfDeath: 2011
      },
      {
        name: 'Jane',
        yearOfBirth: 1912,
        yearOfDeath: 1941
      },
    ]

    const res = people.reduce((prevVal,curVal) => {
      return prevVal.yearOfDeath-prevVal.yearOfBirth < curVal.yearOfDeath-curVal.yearOfBirth ? curVal: prevVal;
    })
    console.log(res.name,res.yearOfDeath-res.yearOfBirth);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.