-2

I have an array with objects and I want to return the object which contains the person who lived the longest. I've gotten this far but I'm stuck now.

const findTheOldest = function(people) {
  for (let i = 0; i < people.length; i++) {
    people[i].yearOfBirth - yearOfDeath
  }
}

const people = [
  {
    name: "Carly",
    yearOfBirth: 1942,
    yearOfDeath: 1970,
  },
  {
    name: "Ray",
    yearOfBirth: 1962,
    yearOfDeath: 2011,
  },
  {
    name: "Jane",
    yearOfBirth: 1912,
    yearOfDeath: 1941,
  },
]
  • Yes. You can use ```reduce()``` – ikhvjs Jun 29 '21 at 13:16
  • 1
    And the problem/question is? Give it a shot (`.reduce()`, or a simple `for` loop)... – Andreas Jun 29 '21 at 13:19
  • @I-Am-Programming, you can use two variables, one is max Age and one is the person with max age. inside a reduce or for loop, if the person is older, you store the maxAge and the person. In the end, you only need to return the person. – ikhvjs Jun 29 '21 at 13:24
  • [Answer here](https://stackoverflow.com/a/34087850/542251) – Liam Jun 29 '21 at 13:24

4 Answers4

1

You could use a reduce as follows:

people.reduce((acc, cur) => !acc || (acc.yearOfDeath - acc.yearOfBirth) < (cur.yearOfDeath - cur.yearOfBirth) ? cur :  acc)
Jazz
  • 341
  • 1
  • 6
0

You simply need to loop over the array of objects and keep storing the person whose age is more than the oldest person you have until now.

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

const findTheOldest = function(people) {
  let oldest = {};
  let oldestAge = 0;
  for (let i = 0; i < people.length; i++) {
    let age = people[i].yearOfDeath - people[i].yearOfBirth
    if( age >= oldestAge) {
       oldestAge = age;
       oldest = people[i];
    }
  }
  return oldest;
}

console.log(findTheOldest(people));
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

You just need to maintain a variable that you can compare with the maximum so far and in the end you'll have the overall maximum. Just return that. You can choose what to do in case more than one person has the same age; this solution returns the first one.

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

function findTheOldest(people) { 

    let diff = 0, res = {}; 
    people.forEach(item => {
        if(diff < item.yearOfDeath - item.yearOfBirth) {
            diff = item.yearOfDeath - item.yearOfBirth;
            res = item;
        }
    });
    
    return res;
}

console.log(findTheOldest(people));
Lakshya
  • 684
  • 6
  • 10
0

Array.protoype.reduce is probably something you are looking for:

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

const findOldest = (p) => p.reduce((acc, e) => {
  const lived = x => x.yearOfDeath - x.yearOfBirth
  return lived(e) > lived(acc) ? e : acc
}, p[0])

console.log(findOldest(people))
ulou
  • 5,542
  • 5
  • 37
  • 47