From bea7415d00877828cdba0a67c3adee818b164e9d Mon Sep 17 00:00:00 2001 From: Akutsang Date: Fri, 3 Feb 2023 23:47:51 +0100 Subject: [PATCH] Given an array of objects representing people with a birth and death year, return the oldest person. --- 12_findTheOldest/findTheOldest.js | 31 ++++++++++++++++++++++++-- 12_findTheOldest/findTheOldest.spec.js | 4 ++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/12_findTheOldest/findTheOldest.js b/12_findTheOldest/findTheOldest.js index 366856a..d2f2b64 100644 --- a/12_findTheOldest/findTheOldest.js +++ b/12_findTheOldest/findTheOldest.js @@ -1,6 +1,33 @@ -const findTheOldest = function() { +const findTheOldest = function(){ + const people = [ + { + name: "Carly", + yearOfBirth: 1942, + yearOfDeath: 1970, + }, + { + name: "Ray", + yearOfBirth: 1962, + yearOfDeath: 2011, + }, + { + name: "Jane", + yearOfBirth: 1912, + yearOfDeath: 1941, + }, + ] + const age = (x) => (x.yearOfDeath || new Date().getFullYear()) - x.yearOfBirth; + const oldest = people.sort((a, b) => + age(a) > age(b) ? -1 : 1 + ); + return oldest[0]; -}; + + + } + + + // 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",