Tell us what’s happening:
I think I have chosen a wrong algorithm to solve the problem, but for me, it’s important to know where is the wrong part and why I passed some test cases and not passed others?
// running tests
sumFibs(1000) should return 1785.
sumFibs(75025) should return 135721.
// tests completed```
Your code so far
function sumFibs(num) {
let fibonacciSeq = [1, 1];
// when I should terminate for execution in order to get the correct sum in all cases?
for (let n = 0, fibonacciSeqSum = 0; fibonacciSeqSum <= num; n++) {
fibonacciSeq.push(
fibonacciSeq[fibonacciSeq.length - 1] +
fibonacciSeq[fibonacciSeq.length - 2]
);
fibonacciSeqSum = fibonacciSeq.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
}, 0);
}
let oddFibonacciSeqSum = fibonacciSeq
.filter((element) => element % 2 !== 0)
.reduce((accumulator, currentValue) => {
return (accumulator <= num)? accumulator + currentValue : accumulator;
// return accumulator+currentValue;
}, 0);
return oddFibonacciSeqSum;
}
sumFibs(4);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Sum All Odd Fibonacci Numbers
Link to the challenge: