Problem 2: Even Fibonacci Numbers Help!

Problem 2: Even Fibonacci Numbers Help!
0

#1

Tell us what’s happening:

Whats wrong with my code Please help me out…
It only passess the second test case that for fiboEvenSum(23)

Your code so far


function fiboEvenSum(n) {
  var a = 0;
  var b = 1;
  var d = 0;
  var sum = 0;
  for(var i = 0 ; i < n; i++){
    d = a + b;
    a = b;
    b = d;
    if(d%2==0){
      sum += d;
    }
    
  }
return sum;
}
console.log(fiboEvenSum(10));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-2-even-fibonacci-numbers


#2

Hey, I think the issue is they defined the sequence as: “1,2,3,5…” whereas your code assumes the sequence is “1,1,2,3…” ?
Just changing your var a = 0 to var a = 1 seemed to fix it for me.


#3

Thanks man… :grinning::grinning::grinning::grinning:


#4

No problem friend :smiley: