FCC not accepting the ans

Tell us what’s happening:

Your code so far


var arr = [];
function sumFibs(num,a=0,b=1) {
if(a%2 === 1){ 
  arr.push(a);
}
if(b <= num){       
  sumFibs(num,b,a+b);
}  
return (a===0) ? arr.reduce((acc,cVal) => acc+cVal) : b;
}

console.log(sumFibs(75025));

Your browser information:

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

Challenge: Sum All Odd Fibonacci Numbers

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

FreeCodeCamp’s test suite is rejecting this code because your function relies upon a global variable, which means you can’t run your function multiple times.

1 Like

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
2 Likes