# Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

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

Your code so far

``````function sumFibs(num) {
let fib =[0,1];
for (let i = 2; i<= num; i++){
let f1 = fib[i-1];
let f2 = fib[i-2];
fib.push(f1+f2);
}
return fib
.filter(element => element % 2 != 0)
.reduce((a,b) => a+b);
}

sumFibs(1785);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36`

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Link to the challenge:

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Tell us what’s happening:
my code is not working for bigger numbers like 1785 …the output comes out to be infinity and also it passes test for some numbers but nor for every number and I cannot seem to figure out what is wrong with code. please help
Your code so far

``````function sumFibs(num) {
let fib = [0, 1];
for (let i = 2; i <= num; i++) {
let f1 = fib[i - 1];
let f2 = fib[i - 2];
fib.push(f1 + f2)
}
return fib
.filter(element => element % 2 != 0)
.reduce((a, b) => a + b);
}

console.log(sumFibs(10));

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36`

Challenge: Intermediate Algorithm Scripting - Sum All Odd Fibonacci Numbers

Link to the challenge:

You can make one modification to your code so that it will pass.

The issue is that `i` is an array index rather than a fibonacci value, so comparing `i` directly to `num` is incorrect logic:

You’ll be pushing values until the array index (rather than the value at that index) is greater than `num`.

Put `console.log(fib)` directly before your `return` statement, to see the results.

If you change the stopping condition so that it checks that the next value to be pushed (i.e. the sum of the two values at the end of your array) will be `<=num`, your array will be correct.

could you please tell me in detail about how should I construct a stopping condition …I can not seem to get my mind around it

We cannot write the code for you.

You need to stop when the most recent Fibonacci number you created is bigger than or equal to the `num`. What might that look like? What does ‘bigger than or equal to `num`’ look like? How do you access your biggest Fibonacci number in your array?

`i<=num` is the condition which determines when your loop will stop. As it stands, the loop will stop when the number of items in your array exceeds `num`, which is not what you’re trying to do.

Instead, you should change this so that the loop stops pushing numbers when the next term in the fibonacci sequence would exceed `num`. You can determine the next term in the sequence by adding together the two values at the end of the array. So with each iteration of the loop that condition should check what the next term would be and that it is `<=num`.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.