diff --git a/findTheOldest/findTheOldest.js b/findTheOldest/findTheOldest.js index b91dfe0..9f35d7a 100644 --- a/findTheOldest/findTheOldest.js +++ b/findTheOldest/findTheOldest.js @@ -1,5 +1,52 @@ -let findTheOldest = function() { +let findTheOldest = function(people) { + let userData = []; + + for (i = 0; i < people.length; i++) { + let date = new Date(); + let aliveStatus = "alive"; + let personAge; + + // if alive check age (today - yearOfBirth) + // if not check age (yearOfDeath - yeahOfBirth) + if ("yearOfDeath" in people[i] === false) { + // aliveStatus = "alive"; + console.log(date.getFullYear()); + console.log(people[i].yearOfBirth); + personAge = Number(date.getFullYear()) - people[i].yearOfBirth; + console.log(personAge); + } else { + personAge = people[i].yearOfDeath - people[i].yearOfBirth; + aliveStatus = "dead"; + } + let personObject = { + name: people[i].name, + age: personAge, + status: aliveStatus + }; + + userData.push(personObject); + } + + console.log(userData); + + let oldestPersonName = userData[0].name; + + for (i = 0; i < userData.length; i++) { + if (i === 0) { + continue; + } else { + if (userData[i].age > userData[i - 1].age) { + oldestPersonName = userData[i].name; + oldestPersonAge = userData[i].age; + console.log("current oldies : " + oldestPersonName); + } + } + } + + personObject = { name: oldestPersonName, age: oldestPersonAge }; + //console.log(personObject); + return personObject; +}; -} module.exports = findTheOldest diff --git a/findTheOldest/findTheOldest.spec.js b/findTheOldest/findTheOldest.spec.js index f7b2ad1..69e6da0 100644 --- a/findTheOldest/findTheOldest.spec.js +++ b/findTheOldest/findTheOldest.spec.js @@ -21,7 +21,7 @@ describe('findTheOldest', function() { ] expect(findTheOldest(people).name).toEqual('Ray'); }); - xit('finds the oldest person if someone is still living', function() { + it('finds the oldest person if someone is still living', function() { const people = [ { name: 'Carly', @@ -40,7 +40,7 @@ describe('findTheOldest', function() { ] expect(findTheOldest(people).name).toEqual('Ray'); }); - xit('finds the oldest person if the OLDEST is still living', function() { + it('finds the oldest person if the OLDEST is still living', function() { const people = [ { name: 'Carly', diff --git a/findTheOldest/test.html b/findTheOldest/test.html new file mode 100644 index 0000000..479f84b --- /dev/null +++ b/findTheOldest/test.html @@ -0,0 +1,12 @@ + + +
+ + + +