diff --git a/12_findTheOldest/findTheOldest.js b/12_findTheOldest/findTheOldest.js index 366856a..df41532 100644 --- a/12_findTheOldest/findTheOldest.js +++ b/12_findTheOldest/findTheOldest.js @@ -1,5 +1,24 @@ -const findTheOldest = function() { - +const findTheOldest = function(people) { + let res; + let maxAge = 0; + let n = people.length; + for(let i = 0 ;i < n ; i++){ + let birth = people[i].yearOfBirth; + let death; + if(people[i].yearOfDeath == undefined){ + const d = new Date() ; + death = d.getFullYear(); + } + else { + death = people[i].yearOfDeath; + } + let age = death - birth; + if(age > maxAge){ + maxAge = age; + res = people[i]; + } + } + return res; }; // Do not edit below this line diff --git a/12_findTheOldest/findTheOldest.spec.js b/12_findTheOldest/findTheOldest.spec.js index 732faaa..dab0d20 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 person with the greatest age if someone is still living', () => { + test('finds the person with the greatest age 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 person with the greatest age if the OLDEST is still living', () => { + test('finds the person with the greatest age if the OLDEST is still living', () => { const people = [ { name: "Carly",