From 8f4b3f9ce92b2d01871551f6da30b5b68f98669b Mon Sep 17 00:00:00 2001 From: Isah Jacob Date: Mon, 31 Oct 2022 23:30:08 +0100 Subject: [PATCH] modified the functions --- 12_findTheOldest/findTheOldest.js | 37 +++++++++++++++++++++++--- 12_findTheOldest/findTheOldest.spec.js | 4 +-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/12_findTheOldest/findTheOldest.js b/12_findTheOldest/findTheOldest.js index 366856a..bc24649 100644 --- a/12_findTheOldest/findTheOldest.js +++ b/12_findTheOldest/findTheOldest.js @@ -1,6 +1,37 @@ -const 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; + }; + + + + const people = [ + { + name: "Carly", + yearOfBirth: 1942, + yearOfDeath: 1970, + }, + { + name: "Ray", + yearOfBirth: 1962, + yearOfDeath: 2011, + }, + { + name: "Jane", + yearOfBirth: 1912, + yearOfDeath: 1941, + }, + ] // Do not edit below this line module.exports = findTheOldest; diff --git a/12_findTheOldest/findTheOldest.spec.js b/12_findTheOldest/findTheOldest.spec.js index 06aec70..75823d6 100644 --- a/12_findTheOldest/findTheOldest.spec.js +++ b/12_findTheOldest/findTheOldest.spec.js @@ -21,7 +21,7 @@ describe('findTheOldest', () => { ] expect(findTheOldest(people).name).toBe('Ray'); }); - test.skip('finds the oldest person if someone is still living', () => { + test('finds the oldest person if someone is still living', () => { const people = [ { name: "Carly", @@ -40,7 +40,7 @@ describe('findTheOldest', () => { ] expect(findTheOldest(people).name).toBe('Ray'); }); - test.skip('finds the oldest person if the OLDEST is still living', () => { + test('finds the oldest person if the OLDEST is still living', () => { const people = [ { name: "Carly",