From 3eab3f0b2692a0c7b66bd165d7eb5922b9fbcea4 Mon Sep 17 00:00:00 2001 From: vfonsah Date: Mon, 7 Sep 2020 11:44:00 +0100 Subject: [PATCH] fibonacci, getTheTitles & findTheOldest excercise challenges --- fibonacci/fibonacci.js | 12 +++++++++--- findTheOldest/findTheOldest.js | 29 ++++++++++++++++++++++++++--- getTheTitles/getTheTitles.js | 8 ++++++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/fibonacci/fibonacci.js b/fibonacci/fibonacci.js index fd597f9..c56cb19 100644 --- a/fibonacci/fibonacci.js +++ b/fibonacci/fibonacci.js @@ -1,5 +1,11 @@ -const fibonacci = function() { +const fibonacci = function (n) { + n = parseInt(n); -} + let fib = [, 1, 1]; + for (let i = 3; i <= n; i++) { + fib[i] = fib[i - 1] + fib[i - 2]; + } + return n <= 0 ? "OOPS" : fib[n]; +}; -module.exports = fibonacci +module.exports = fibonacci; diff --git a/findTheOldest/findTheOldest.js b/findTheOldest/findTheOldest.js index b91dfe0..827248a 100644 --- a/findTheOldest/findTheOldest.js +++ b/findTheOldest/findTheOldest.js @@ -1,5 +1,28 @@ -let findTheOldest = function() { +let findTheOldest = function (people) { + // people.forEach((person) => { + // this.lifeSpan = + // person.yearOfDeath - person.yearOfBirth || Date("y") - person.yearOfBirth; + // }); -} + return people.reduce((oldestPerson, currentPerson) => { + const oldestAge = getPersonAge( + oldestPerson.yearOfBirth, + oldestPerson.yearOfDeath + ); + const currentAge = getPersonAge( + currentPerson.yearOfBirth, + currentPerson.yearOfDeath + ); -module.exports = findTheOldest + return currentAge > oldestAge ? currentPerson : oldestPerson; + }); +}; + +const getPersonAge = (birthYear, deathYear) => { + if (!deathYear) { + deathYear = new Date().getFullYear(); + } + return deathYear - birthYear; +}; + +module.exports = findTheOldest; diff --git a/getTheTitles/getTheTitles.js b/getTheTitles/getTheTitles.js index 2b52aa0..15f8894 100644 --- a/getTheTitles/getTheTitles.js +++ b/getTheTitles/getTheTitles.js @@ -1,5 +1,9 @@ -const getTheTitles = function() { - +const getTheTitles = function(books) { + let titles = [] + books.forEach(book => { + titles.push(book.title); + }); + return titles } module.exports = getTheTitles;