fibonacci, getTheTitles & findTheOldest excercise challenges

This commit is contained in:
vfonsah 2020-09-07 11:44:00 +01:00
parent 980018577a
commit 3eab3f0b26
3 changed files with 41 additions and 8 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;