Tell us what’s happening:
Describe your issue in detail here.

Hello fellows Campers,

I’m actually doing the “Sum All Odd Fibonacci Numbers” exercise in the JavaScript course.

I thought my proposition for this exercise would be correct, but it seems there is at least one mistake somewhere in my code. What I don’t understand is why the code runs correctly for all check test, except when num = 4000000 and when num = 75025, it doesn’t return the expected value (4613732 and 135721).

Can somebody tell me where is this mistake, or lead me to where I have to look for it ?
Thank you for your help !

**Your code so far**

function sumFibs(num) {
const fiboArr = [1, 1]; // Fibonacci always begin with 1 and 1
let oddArr = []; // Declaring empty array for the odd values
let fiboValue = 0; // This variable will create the Fibonacci suite by summing the two last number and pushing the sum in the fiboArr
let oddValue = 0; // This variable will sum only the odd values from the Fibonacci suite
for (let i = 1; i <= num; i++) {
fiboValue = fiboArr[i] + fiboArr[i - 1]; // Adding the two last numbers in fiboArr together
fiboArr.push(fiboValue); // Pushing the previous sum to the end of fiboArr
oddArr = fiboArr.filter(elem => elem <= i && elem % 2 !== 0); // oddArr getting only the odd values that are less than num
oddValue = oddArr.reduce((a, b) => a + b); // oddValue take the value of all oddArr summed together
}
return oddValue;
}
sumFibs(4);

Thank you for your answer Jeremy !
I’ve just checked the solutions proposed by FCC, and the second one also declare the two first values of the Fibonacci suite in an array.
I can’t understand why this is causing the code to fail…

Because you are making Fibonacci values that are larger than num. When you try to run the failing tests, you should see the error message

Potential infinite loop detected on line 9. Tests may fail if this is not changed.

Computing 4 million Fibonacci values takes a while, and you end up with massive numbers that overflow. You fix this by not computing all the excess values.

Ok, I will look and think on how I can do it this way, thank you for your help

EDIT : After spending some time looking for a way to modify my code to solve this exercise, I can’t figure out if it is even possible to complete the challenge with what I wrote, and I can’t figure out how to do it if it can be done.
I am pretty sure this is a way to do it, and I don’t want to just copy the solution proposed by FCC if my code can complete it.

I didn’t say your code won’t work. Most of it will work. The only part that won’t is your for loop part. You could ‘hack’ the for loop to break at the right point, but you’d be turning it into a messy while loop that way. Better to just use a while loop almost the same body as you currently have.