odin-javascript-exercises/12_findTheOldest/solution/findTheOldest-solution.js

20 lines
494 B
JavaScript
Raw Normal View History

2022-02-20 11:07:44 -08:00
const findTheOldest = function (array) {
2023-01-21 09:53:41 -08:00
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
const currentAge = getAge(
currentPerson.yearOfBirth,
currentPerson.yearOfDeath
);
return oldestAge < currentAge ? currentPerson : oldest;
});
2022-02-20 11:07:44 -08:00
};
const getAge = function (birth, death) {
2023-01-21 09:53:41 -08:00
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
2022-02-20 11:07:44 -08:00
};
module.exports = findTheOldest;