Nothing Checks Off In The Fibonacci Numbers Problem

I’m trying to complete the Fibonacci Numbers Problem in the Project Euler Course my code is working on Replit but won’t pass any of the tests in the FreeCodeCamp IDE not even the test that checks if my answer is a number, even though I use console.log() to verify my return is one.

function fiboEvenSum(n) {
  let fibArray = [1, 2]
  console.log('at first array is ' + fibArray)
  while (fibArray[fibArray.length - 1] < n){
    console.log(fibArray[fibArray.length - 1])
    let num2 = fibArray[fibArray.length - 1]
    let num1 = fibArray[fibArray.length - 2]
    console.log('num 1 = ' + num1)
    console.log('num 2 = ' + num2)
    fibArray.push(num1 + num2)
    console.log('now is ' + fibArray)
  }
  if(fibArray[fibArray.length - 1] % 2 != 0){
    delete fibArray[fibArray.length - 1]
  }
  console.log('final fib array = ' + fibArray)
  evenList = []
  for (let i = 0; i < fibArray.length; i++) {
    if(fibArray[fibArray.length - i] % 2 === 0) {
      evenList.push(fibArray[fibArray.length - i])
      console.log('Even list = ' + evenList)
    }
    console.log('cycle ' + i)
  }
  console.log('Even list after = ' + evenList)
  let sum = 0
  for(let w = 0; w < evenList.length; w++){
    sum += evenList[w];
    console.log('sum = ' + sum)
  }
  sum = +sum
  console.log(typeof sum)
  console.log('Final sum = ' + sum)
  console.log(typeof sum)
  return sum;
}

I would make a sample function call and check the error messages.

Also, you’ll probably need to comment out the console logs to avoid tripping the infinite loop protection.

1 Like

It’s still not working

hows that not working? which code snippet that used? what is your expected output from this attempted “snippet”, and where its (you might find probable) failing?

describing or emphasizing on those notes is more useful and helpful to debug any coding related queries

happy coding :slight_smile:

1 Like

Which errors did you see and fix when you ran your function in the freeCodeCamp editor?

1 Like

Yes, that is the output from the test suite.

Copy one of those function calls into the editor.

If you

then you should see

at first array is 1,2
2
num 1 = 1
num 2 = 2
now is 1,2,3
3
num 1 = 2
num 2 = 3
now is 1,2,3,5
5
num 1 = 3
num 2 = 5
now is 1,2,3,5,8
8
num 1 = 5
num 2 = 8
now is 1,2,3,5,8,13
final fib array = 1,2,3,5,8,
ReferenceError: assignment to undeclared variable evenList
1 Like

I didn’t notice evenList was undeclared, it works now that I fixed it thanks for the help

Couple of comments by the way

function fiboEvenSum(n) {
  let fibArray = [1, 2]; // should use const here
  console.log('at first array is ' + fibArray); // should comment these out when running the tests
  while (fibArray[fibArray.length - 1] < n) {
    console.log(fibArray[fibArray.length - 1]);
    let num2 = fibArray[fibArray.length - 1];
    let num1 = fibArray[fibArray.length - 2];
    console.log('num 1 = ' + num1);
    console.log('num 2 = ' + num2);
    fibArray.push(num1 + num2);
    console.log('now is ' + fibArray);
  }
  if (fibArray[fibArray.length - 1] % 2 != 0) {
    delete fibArray[fibArray.length - 1];
  }
  console.log('final fib array = ' + fibArray);
  evenList = []; // should use const here too
  for (let i = 0; i < fibArray.length; i++) {
    if (fibArray[fibArray.length - i] % 2 === 0) {
      evenList.push(fibArray[fibArray.length - i]);
      console.log('Even list = ' + evenList);
    }
    console.log('cycle ' + i);
  }
  console.log('Even list after = ' + evenList);
  let sum = 0;
  for (let w = 0; w < evenList.length; w++) {
    sum += evenList[w];
    console.log('sum = ' + sum);
  }
  sum = +sum; // What does this do?
  console.log(typeof sum);
  console.log('Final sum = ' + sum);
  console.log(typeof sum);
  return sum;
}

console.log(fiboEvenSum(10));

Euler problems are all about efficiency. I’d see if you can modify your solution to use zero arrays. You should only need 3-4 variables, each holding a single number.

1 Like