odin-javascript-exercises/findTheOldest/findTheOldest.js

17 lines
467 B
JavaScript
Raw Normal View History

2019-04-11 10:11:03 -07: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