From 6e12c2451a874f35efc6b5efb635708d0be75917 Mon Sep 17 00:00:00 2001 From: Muhammad Sameem Date: Tue, 12 Mar 2024 11:55:20 +0400 Subject: [PATCH] :heavy_check_mark: exercise findTheOldest --- .../12_findTheOldest/findTheOldest.js | 17 ++++++++++++-- .../12_findTheOldest/findTheOldest.spec.js | 22 +++++++++---------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.js b/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.js index 366856a..16f318a 100644 --- a/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.js +++ b/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.js @@ -1,6 +1,19 @@ -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; +}; // Do not edit below this line module.exports = findTheOldest; diff --git a/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.spec.js b/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.spec.js index aac207c..c3b7a64 100644 --- a/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.spec.js +++ b/JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.spec.js @@ -1,7 +1,7 @@ -const findTheOldest = require('./findTheOldest') +const findTheOldest = require("./findTheOldest"); -describe('findTheOldest', () => { - test('finds the person with the greatest age!', () => { +describe("findTheOldest", () => { + test("finds the person with the greatest age!", () => { const people = [ { name: "Carly", @@ -18,10 +18,10 @@ describe('findTheOldest', () => { yearOfBirth: 1912, yearOfDeath: 1941, }, - ] - expect(findTheOldest(people).name).toBe('Ray'); + ]; + 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", @@ -37,10 +37,10 @@ describe('findTheOldest', () => { yearOfBirth: 1912, yearOfDeath: 1941, }, - ] - expect(findTheOldest(people).name).toBe('Ray'); + ]; + 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", @@ -56,7 +56,7 @@ describe('findTheOldest', () => { yearOfBirth: 1912, yearOfDeath: 1941, }, - ] - expect(findTheOldest(people).name).toBe('Carly'); + ]; + expect(findTheOldest(people).name).toBe("Carly"); }); });