Sum All Odd Fibonacci Numbers question

Sum All Odd Fibonacci Numbers question
0

#1

Tell us what’s happening:
I have tried to print the sequence and all hell breaks loose. I commented out the respective console.log. But I still get insane numbers for the sum. What am I doing wrong?

Your code so far


function sumFibs(num) {
  function getFibSec(num) {
    var sec = [];
    if (num==1) {
      console.log("returning [1]");
      return [1];
    } else if (num==0) {
      console.log("returning No Zeros");
      return "No Zeros"
    } else {
      sec = [1,1];
      if (num ==2) {
        console.log("returning [1,1]");
        return sec;
      }
      for (let i=1; i<num-1;i++) {
        sec.push(sec[i-1]+sec[i]);
      }
      //console.log("returning sec " + sec);
      return sec;
    }

    
  }
  //console.log("function returns " + getFibSec(50));
  var gottenSec = getFibSec(num);
  var sum=0;
  for (let j=0;j<gottenSec.length;j++) {
    if (gottenSec[j]%2!==0) {
    sum += gottenSec[j];
    }
  }
  console.log("sum is " + sum);
  return sum
}

sumFibs(1000);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; 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

You should walk through your code line by line for a call like sumFibs(10) to first figure out why your internal function getFibSec function returns the following array:

[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]

instead of

[ 1, 1, 2, 3, 5, 8]

Your getFibSec should be creating an array of numbers which are less than or equal to 10, instead of the first 10 numbers in the Fibonacci sequence.


#3

That was it. Thank you!