odin-javascript-exercises/12_findTheOldest/solution/findTheOldest-solution.js
2023-01-21 12:53:41 -05:00

19 lines
494 B
JavaScript

const findTheOldest = function (array) {
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
const currentAge = getAge(
currentPerson.yearOfBirth,
currentPerson.yearOfDeath
);
return oldestAge < currentAge ? currentPerson : oldest;
});
};
const getAge = function (birth, death) {
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
};
module.exports = findTheOldest;