odin-js-fundamentals-part-5/12_findTheOldest/solution/findTheOldest-solution.js

20 lines
494 B
JavaScript
Raw Permalink Normal View History

2024-01-11 08:52:05 +00:00
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;