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

20 lines
494 B
JavaScript
Raw Normal View History

2022-02-20 19:07:44 +00:00
const findTheOldest = function (array) {
2023-01-21 17:53:41 +00: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 19:07:44 +00:00
};
const getAge = function (birth, death) {
2023-01-21 17:53:41 +00:00
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
2022-02-20 19:07:44 +00:00
};
module.exports = findTheOldest;