fibonacci, getTheTitles & findTheOldest excercise challenges
This commit is contained in:
parent
980018577a
commit
3eab3f0b26
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
||||
return currentAge > oldestAge ? currentPerson : oldestPerson;
|
||||
});
|
||||
};
|
||||
|
||||
const getPersonAge = (birthYear, deathYear) => {
|
||||
if (!deathYear) {
|
||||
deathYear = new Date().getFullYear();
|
||||
}
|
||||
return deathYear - birthYear;
|
||||
};
|
||||
|
||||
module.exports = findTheOldest
|
||||
module.exports = findTheOldest;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue