Intermediate Algorithm Scripting: Sum All Odd Fibonacci Numbers

My code below returns the expected results in VS code when I pass the given ‘num’ values in test cases. But when I try it in FCC, the code passes only the first test case and fails the rest. What is happening?

function sumFibs(num, n) {
  const fib = [0, 1];
  for (let i = 2; i <= n; i++) {
    fib[i] = fib[i - 2] + fib[i - 1];
  const odds = fib.filter((el) => el % 2 == 1 && el <= num);
  const sum = odds.reduce((acc, curr) => acc + curr);
  return sum;

sumFibs(4000000, 300);

Check at the FCC tests how the function is supposed to be called and how you’ve written it at vscode.

In the function that you have written, you accept two parameters, num and n. What is the purpose of each of them?

Link to FCC challenge for reference:

‘num’ is the value mentioned in the challenge, the max value where we should limit the Fibonacci series. ‘n’ is the number that I used to construct the actual Fibonacci series. The solutions do not have this second argument but I followed such an approach.

You can’t randomly add required arguments. When you do that, the code cannot pass.

The tests really, really need you to keep the same function signature.

1 Like

the second argument in my code, 300 in this case, is the ‘n’ value I used to construct the actual Fibonacci series. It does not affect the result in VS code. I tried with the given ‘num’ values in FCC test cases in VS code, and all returned expected values.

Your code will not work if no n is supplied, and the tests don’t supply a value for n. You don’t need one anyways.

1 Like

Ok, then I will modify the approach and use only the ‘num’ parameter. Thanks for the clarification.

1 Like

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