From 3ecafbedae2d5ec7be3cf177a9b184cd2f81101b Mon Sep 17 00:00:00 2001 From: Mayar-Hassanin98 <48419750+Mayar-Hassanin98@users.noreply.github.com> Date: Sat, 25 Jul 2020 17:54:32 +0200 Subject: [PATCH] Update findTheOldest.js --- findTheOldest/findTheOldest.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/findTheOldest/findTheOldest.js b/findTheOldest/findTheOldest.js index b91dfe0..7fd2f42 100644 --- a/findTheOldest/findTheOldest.js +++ b/findTheOldest/findTheOldest.js @@ -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