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 @@ + + + + + + + Document + + + + + \ No newline at end of file diff --git a/findTheOldest/test.js b/findTheOldest/test.js new file mode 100644 index 0000000..338a050 --- /dev/null +++ b/findTheOldest/test.js @@ -0,0 +1,68 @@ +function findTheOldest (people) { + // console.log(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 luckyPerson = calculateAge(userData); + console.log(luckyPerson); +} + +function calculateAge(userData) { + + let oldestPersonName = userData[0].name; + // let oldestPersonAge = userData[0].age; + + for (i = 0; i < userData.length; i++) { + if (i === 0) { continue; } + else { + if (userData[i].age > userData[i-1].age ) { + oldestPersonName = userData[i].name; + console.log("current oldies : " + oldestPersonName); + } + } + } + return oldestPersonName; +} + +const people = [ + { + name: "Carly", + yearOfBirth: 2018 + }, + { + name: "Ray", + yearOfBirth: 1962, + yearOfDeath: 2011 + }, + { + name: "Jane", + yearOfBirth: 1912, + yearOfDeath: 1941 + } +]; + +findTheOldest(people); \ No newline at end of file