Sum All Odd Fibonacci Numbers

function sumFibs(num) {

let sum = 0;  
for (let i = 0; i < num.length; i++){
  if (num % 2 == 0) {

  } else if (num % 2 == 1){
    sum = num + num
  }
}
  return sum;
}

//Fibonacci sequence is to add first two numbers, answer takes up next spot, then repeat
console.log(sumFibs(4));

Need help. Need to find a way to tell them to add all the odd numbers in the sequence. Do I need to write a line of code to tell them what the sequence is? Or is the Fibonacci sequence already known for this problem?

share that step url as well, thanks :slight_smile:

Sorry about that, thanks for reminding me

Yes, you will need to find an approach to generate the Fibonacci numbers you need.

function sumFibs(num) {

// firstTerm =  0
// secondTerm = 1

// nextTerm = firstTerm + secondTerm; (0 + 1)
// firstTerm = secondTerm; (1)
// secondTerm = nextTerm; (1)

// nextTerm = firstTerm + secondTerm; (1 + 1)


sumFibs(4);

I understand what the pattern is in the Fibonacci sequence but I am stuck when it comes to writing the code for it

It looks like you’ve got the correct logical flow. But you need to keep running that logic, until a condition is met that tells your code to stop. Do you know the syntax for something like that? (There’s a few options here)

function sumFibs(num) {

// firstTerm =  0
// secondTerm = 1

// nextTerm = firstTerm + secondTerm; (0 + 1)
// firstTerm = secondTerm; (1)
// secondTerm = nextTerm; (1)

// nextTerm = firstTerm + secondTerm; (1 + 1)
// when secondTerm <= num...STOP

for (let i = 0; i <= num; i++){
  
}


sumFibs(4);

Do you mean something like this?

That looks like a good starting point, yes!

1 Like
function sumFibs(num) {

// firstTerm =  0
// secondTerm = 1

// nextTerm = firstTerm + secondTerm; (0 + 1)
// firstTerm = secondTerm; (1)
// secondTerm = nextTerm; (1)

// nextTerm = firstTerm + secondTerm; (1 + 1)
// when secondTerm <= num...STOP


for (let i = 0; i <= num; i++){
  if (i + (i + 1) == (i + 2)) {
    (i + 1) + (i + 2) == (i + 3)
  }
}

console.log(sumFibs(4));

I’m trying to get it to understand the pattern but I’m almost positive I am taking the wrong approach. Been stuck on the next steps

how about you try to put them in more “obvious” way, and see if you can spot that sequence going more easily, here is an example of first “seven” numbers from “fibonacci series”

0 + 0 = 0
1 + 0 = 1
1 + 1 = 2
2 + 1 = 3
3 + 2 = 5
5 + 3 = 8
8 + 3 = 11

hope this helps, happy learning :slight_smile:

2 Likes
function sumFibs(num) {
  let prevNumber = 0;
  let currNumber = 1;
  let result = 0;

  // before number reaches num 
  while (currNumber <= num) {
    // if that number is odd
    if (currNumber % 2 == 1) {
      // then those odd numbers to the result [1+1+3] = 5
      result += currNumber;
    } // let them know how the Fibonacci sequence works
    currNumber += prevNumber;// 1 = 0 + 1 aka 0 + 1 = 1
    prevNumber = currNumber - prevNumber; // 0 = 1 - 0; letting them know what the previous number is creating the cycle/pattern
  }

  return result;
}

sumFibs(4);

What do you think?

I always get a little confused when I see a big jump in the complexity of your solution relative to your described plan, but this is a clever trick here.

This is great! The other common solution pattern is a functional approach using array methods. Something like this:

getAllFibsLte(num)
  .filter(isOdd)
  .reduce(sum)

(you’d need to implement the getAllFibsLte, isOdd and sum functions).

The trade-off compared to your imperative solution is reduced performance due to higher memory consumption and more loops, but is arguably more legible (it reads like the English instructions).

1 Like