Sum All Odd Fibonacci Numbers bug

Tell us what’s happening:
I created this solution and tested it in my browser. It passes all the requirements except the first one ( which I fixed that and tried it as well, no luck with that either).
Can anyone explain to me what is the problem with my solution?

Your code so far

function sumFibs(num) {
// generate fibonacci series of the the numbers from 1 to the passed value
    for(var fibArray = [1], i=0,j=1,k=0; k<num;i=j,j=x,k++ ){
    return fibArray
// filter the numbers in the description and return the sum
        .filter (x => x<num && x%2 !==0)
        .reduce((a,b) => a+b)


Your browser information:

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

Challenge: Sum All Odd Fibonacci Numbers

Link to the challenge:

I am getting x is not defined @Rezathema
can you define that variable?

1 Like

The one which generates the Fibonacci sequence? I tried even debugging it in Dev tool, I could not face any issues. Only problem with the code above is that it does not return a number if [1] is passed into it @GameMonk

The editor says

ReferenceError: assignment to undeclared variable x

Your code has

You did not declare x in the head of your for loop. You cannot use a variable you did not declare in the freeCodeCamp editor, but some other environments let you get away with not declaring all of your variables. Adding x to your declared variables will fix this.

Though, this is not a particularly ideomatic use of a for loop and its somewhat unreadable, so I’d recommend making a cleaner loop head.


I’m fascinated by that for loop, I’ve never seen anyone using it like that, especially for a simple Fibonacci array generator. Would you mind elaborating (in pseudo code) what it’s in theory supposed to do? Why all the variables i, j, k and x?

The basic format of a for loop is

for (initialization; continuation condition; incrementing) 

You can put multiple constructs in the initialization and incrementing places, but a loop like this is very much not recommended and would not work as well without var, which is also not recommended.

Yeah I know that, and yeah that loop is unreadable and its functionality in its current state depends on the browser’s kindness, I was just wondering what was the reasoning behind it. Putting significant parts of the logic into the loop declaration instead of the body is something you don’t see every day.

I got the code for Fibonacci series from stackoverflow, but Basically the logic is the same right?

function fib(n){
  let arr = [0, 1];
  for (let i = 2; i < n + 1; i++){
    arr.push(arr[i - 2] + arr[i -1])
 return arr[n]

Quite interesting that a browser would let me get away with such an error. I was not aware of this.
but let’s say with the simplified for loop I want to generate Fibonacci sequence and then filter it with code I used above. How can I achieve that? ex explanation would be greatly appreciated.
Thank you for helping out

Thank you for the info. I still have some confusion regarding what caused the issue.
I had no idea the browser can ignore such errors

Some environments have Strict Mode enabled by default. It’s recommended for all professional software projects nowadays.

You can do the same filter and reduce on the array you get from the above loop as well. Though, for this challenge I recommend that people don’t even store an array of Fibonacci numbers. The general rules for fast/clean code are

  1. Don’t compute a value if you don’t need it
  2. Only store a value if you need it more than once

In this case, you only need to use the Fibonacci numbers once to add up the odd ones, so its faster and cleaner to add them up as you generate them.

1 Like

Oh thanks for the info Jeremy. I’ll make sure to follow these rules from now on.