Sum All Odd Fibonacci Numbers Debugging

Sum All Odd Fibonacci Numbers Debugging
0

#1

Tell us what’s happening:
My code won’t pass the test. I wonder why?

Your code so far


function fibs(ordinal) {
  if (ordinal === 1 || ordinal === 2) {
    return 1;
  }
  return fibs(ordinal - 2) + fibs(ordinal - 1);
}

function sum(numArr) {
  let result = numArr.reduce((x, y) => x + y);
  return result;
}

function sumFibs(num) {
  let arr = [];
  for (let i = 1; fibs(i) <= num; i++) {
    if ((fibs(i) % 2) === 1) {
      arr.push(fibs(i));
    }
  }

  return sum(arr);
}

sumFibs(4);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers


#2

Try reloading the browser and clearing cache? Code runs fine for me. Only thing I can possibly think is that it’s timing out (avoid recursive functions in JS, in particular non-tail-recursive functions, and in particular always avoid recursion in JS if you’re doing do lots of calculation and generating large numbers)


#3

The problem is solved. Thank you very much.