Tell us what’s happening:
I wrote a working solution. But it somehow does not pass the test for 4000000. Although on repl it works without problems.
For some reason, the function that generates an array of Fibonacci numbers stops before the terminal condition is satisfied
Your code so far
function sumFibs(num) {
function fib(n) {
return n <= 1 ? n : fib(n - 1) + fib(n - 2);
};
const fibArrMake = (n) => {
let result = [];
for (let i = 0; ; i++) {
if (fib(i) > n) {
break;
}
result.push(fib(i));
}
return result;
};
const fibArr = fibArrMake(num);
const filtered = fibArr.filter(el => el % 2);
const sum = filtered.reduce((acc, el) => acc + el, 0);
return sum;
}
sumFibs(4);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers