FCC Bonfire Series 130: Sum All Odd Fibonacci Numbers

Leonardo Fibonacci. He loves JavaScript!

Oh, good old Fibonacci! Today, we are going to be working with this marvelous integer series. For those not familiar with it, you can check what the Fibonacci sequence is here. For those interested, the guy in the picture (Leonardo Fibonacci) came up with it. He loved math and was from Pisa, Italy.

Let’s do a quick recap. The Fibonacci sequence is formed by summing the two previous numbers to obtain the next. We start off like so:

0, 1, (0 + 1), (1 + (0 + 1)), ((0 + 1) + (1 + (0 + 1))… but we can see it more clearly like so: 0, 1, 1, 2, 3, 5, 8, (8 + 5), etc.

This bonfire will have us sum all odd Fibonacci numbers up to and including the given number (if it is a Fibonacci number). For example:

// Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

sumFibs(4) // -> 0 + 1 + 1 + 3 = 5

sumFibs(9) // -> 0 + 1 + 1 + 3 + 5 = 10

And so on. Given this information, the conclusion is obvious: we need a way to identify the three requisites for a number to be added:

  • Is Fibonacci number.
  • Is an odd number.
  • Is lower than or equal to the given number.

Let’s get to coding! We’ll start by getting the Fibonacci numbers up to the given number, once that’s out of the way, the task at hand will become trivial (we would just need to check if it’s an odd number!).

We are going to create 3 variables. It sounds crazy, I know, but we’ll need them. The first two will represent the two numbers before the current Fibonacci number (we need to add these to get the next in the series) and the third variable will represent the next Fibonacci number itself. Using a while loop, we are going to sum these two (as long as the sum is below the given number) and update them. It’s easier seen in plain code:

function sumFibs(num) {
  var first = 0,  // I set these three to the first three numbers in the series.
      second = 1,
      fibonacci = 1;

  while (first + second <= num) {
    fibonacci = first + second;  // Next Fibonacci is the sum of previous 2
    first = second;              // We set first to be the next item in the sequence.
    second = fibonacci;          // We set second to be the current fibonacci.
                                 // We'll use it to get the NEXT Fibonacci
                                 // number in the next iteration of the while loop.
    console.log(fibonacci);
  }
}

This function doesn’t sum the odd Fibonacci as you may notice. In fact, it doesn’t sum anything at all! But we’ve gotten it to print the Fibonacci sequence up to the number we pass it as an argument. The rest will be much easier!

Let us continue by adding a new variable, where we’ll keep track of the final result. We’ll also use an if statement to check if the current Fibonacci number is odd (remember that we can use the modulo (%) operator to check for the remainder of a division). This is what we can come up with:

function sumFibs(num) {
  var first = 0,
      second = 1,
      fibonacci = 1,
      sum = 1; // We set this to one, since we are skipping the first 1 in the sequence.

  while (first + second <= num) {
    fibonacci = first + second;
    first = second;
    second = fibonacci;
    if (fibonacci % 2 !== 0) {  // If a number % 2 is 0, the number is even, else, it is odd!
      sum += fibonacci;  // Same as sum = sum + fibonacci;
    }
  }

  return sum; // We return the total sum in hopes that it is correct.
}

And this is, camper friends, the final version of the Sum All Odd Fibonacci Numbers bonfire. As always, I appreciate that you come up with your own unique solution to this task, since that’s what will help you understand and know how to overcome what comes next!

Don’t be shy and share your results, feedback is the joy of this industry!