Fibonacci sequence help

Tell us what’s happening:

Hi. Right now I’m just trying to recreate the Fibonacci sequence for 10, but it’s not working. I was hoping to just get a hint to point me in the right direction. Thank you.

Your code so far


/* function isOdd(value){
   if (value%2 != 0)
       return true;
   else
       return false;
} */

function sumFibs(num) {
 var holder = [];
 for (var i = 0; i <= num; i++){
   var start = 1;
   start = start + i;
   holder.push(start);
}
return holder;
}

console.log(sumFibs(10));

Your browser information:

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

Challenge: Sum All Odd Fibonacci Numbers

Link to the challenge:

you are doing the Fibonacci sequence wrong:
each number is the sum of the two previous numbers
so, starting with [1,1] you add 2 (1+1), then 3 (1+2), then 5 (2+3), then 8 (3+5), then 13 (5+8) and so on

you first declare start inside the loop so it will be redeclared each time the loop run and never increase, plus you use start = start + i, which is not at all how you create a Fibonacci sequence

2 Likes

Thanks for the feedback, I spent some time to better understand exactly what needs to happen and your examples helped. I updated to the following which I think works.

/* function isOdd(value){
   if (value%2 != 0)
       return true;
   else
       return false;
} */

function sumFibs(num) {
 var holder = [1, 1];
 for (var i = 0; i <= num; i++){
      holder.push(holder[holder.length-2] + holder[holder.length-1]);
}
return holder;
}

console.log(sumFibs(10));

are you getting only numbers that are less or equal to num?

seems you have more than that

return the sum of all odd Fibonacci numbers that are less than or equal to num .

1 Like

What do you see in your console with that solution?

1 Like

Thanks, once I was able to get the Fibonacci sequence going I was able to solve the rest

function isOdd(value){
   if (value%2 != 0)
       return true;
   else
       return false;
} 

function sumFibs(num) {
 var holder = [1, 1];
 for (var i = 0; i <= num; i++){
      var tiger = holder[holder.length-2] + holder[holder.length-1]
      if (tiger <= num ){holder.push(tiger)};
      var lion = holder.filter(element => isOdd(element) === true).reduce((a, b) => a + b, 0)
}
return lion;
}

console.log(sumFibs(75025));
1 Like

I’m glad to hear that you figured it out!

1 Like