From d538ffeb27b58e6708daf0ca4b9bacc44fd8cc0e Mon Sep 17 00:00:00 2001
From: Roberra0 <roberra.aklilu@gmail.com>
Date: Fri, 7 Jul 2023 19:15:27 -0700
Subject: [PATCH] completed final exercise

---
 12_findTheOldest/findTheOldest.js      | 18 +++++++++++++++---
 12_findTheOldest/findTheOldest.spec.js |  4 ++--
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/12_findTheOldest/findTheOldest.js b/12_findTheOldest/findTheOldest.js
index 366856a..89d01be 100644
--- a/12_findTheOldest/findTheOldest.js
+++ b/12_findTheOldest/findTheOldest.js
@@ -1,6 +1,18 @@
-const findTheOldest = function() {
-
-};
+const findTheOldest = function(people){
+    peopleSorted = people.sort((a, b)=>
+        (getAge(a) > getAge(b)) ?
+            -1:
+            1
+    );
+    return(peopleSorted[0]);
+}
 
+function getAge(person){
+    let death = person.yearOfDeath;
+    if(!death){
+        death = new Date().getFullYear();
+    };
+    return death - person.yearOfBirth;
+}
 // 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",