algorithms/sumFibs.js
2024-01-12 07:01:06 -05:00

20 lines
402 B
JavaScript

function sumFibs(num) {
if (num === 1) {
return 1;
}
let fibs = [1, 1];
while (fibs[fibs.length - 1] <= num) {
fibs.push(fibs[fibs.length - 1] + fibs[fibs.length - 2]);
}
if (fibs[fibs.length - 1] > num) {
fibs.pop();
}
let odds = fibs.filter((n) => n % 2);
return odds.reduce((accumulator, currentValue) => accumulator + currentValue);
}
console.log(sumFibs(1000));