2021-04-30 10:12:52 +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
|
|
|
|
})
|
2021-05-10 08:08:31 +00:00
|
|
|
};
|
2019-04-11 17:00:21 +00:00
|
|
|
|
2021-04-30 10:12:52 +00:00
|
|
|
const getAge = function(birth, death) {
|
|
|
|
if (!death) {
|
|
|
|
death = new Date().getFullYear();
|
|
|
|
}
|
|
|
|
return death - birth;
|
2021-05-10 08:08:31 +00:00
|
|
|
};
|
2019-04-11 17:00:21 +00:00
|
|
|
|
2021-05-10 08:08:31 +00:00
|
|
|
module.exports = findTheOldest;
|