From 22a6289b629e4b5d7da5bda608f32e6eb8cd3881 Mon Sep 17 00:00:00 2001 From: Thuan Bui <9248622+10h30@users.noreply.github.com> Date: Wed, 26 Feb 2025 11:16:51 +0900 Subject: [PATCH] Completed excercise 12 --- 12_findTheOldest/findTheOldest.js | 27 +++++++++++++++++++++++++- 12_findTheOldest/findTheOldest.spec.js | 4 ++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/12_findTheOldest/findTheOldest.js b/12_findTheOldest/findTheOldest.js index 366856a..b4b5679 100644 --- a/12_findTheOldest/findTheOldest.js +++ b/12_findTheOldest/findTheOldest.js @@ -1,5 +1,30 @@ -const findTheOldest = function() { +const findTheOldest = function(people) { + console.log(people) + const sortByAge = people.sort((a,b) => { + + if (!a.yearOfDeath) { + let currentYear = new Date().getFullYear(); console.log(currentYear) + ageOfa = currentYear - a.yearOfBirth + } + else { + ageOfa = a.yearOfDeath - a.yearOfBirth + } + console.log(a.name + ": " + ageOfa) + if (!b.yearOfDeath) { + let currentYear = new Date().getFullYear(); + ageOfb = currentYear - b.yearOfBirth + } + else { + ageOfb = b.yearOfDeath - b.yearOfBirth + } + console.log(b.name + ": " + ageOfb) + + return ageOfa < ageOfb ? 1 : -1 + }) + const oldest = sortByAge[0] + console.log(oldest); + return oldest; }; // Do not edit below this line diff --git a/12_findTheOldest/findTheOldest.spec.js b/12_findTheOldest/findTheOldest.spec.js index aac207c..e1b234f 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 yearOfDeath field is undefined on a non-oldest person', () => { + test('finds the oldest person if yearOfDeath field is undefined on a non-oldest person', () => { const people = [ { name: "Carly", @@ -40,7 +40,7 @@ describe('findTheOldest', () => { ] expect(findTheOldest(people).name).toBe('Ray'); }); - test.skip('finds the oldest person if yearOfDeath field is undefined for the oldest person', () => { + test('finds the oldest person if yearOfDeath field is undefined for the oldest person', () => { const people = [ { name: "Carly",