Update findTheOldest.js

This commit is contained in:
Mayar-Hassanin98 2020-07-25 17:54:32 +02:00 committed by GitHub
parent 888383a44e
commit 3ecafbedae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 2 deletions

View File

@ -1,5 +1,15 @@
let findTheOldest = function() { 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 module.exports = findTheOldest