**The Challenge:**

1 2 3 4 5 |
Return the sum of all odd Fibonacci numbers up to and including the passed number if it is a Fibonacci number. The first few numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8, and each subsequent number is the sum of the previous two numbers. As an example, passing 4 to the function should return 5 because all the odd Fibonacci numbers under 4 are 1, 1, and 3. |

**The solution should return:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
sumFibs(1) should return a number. sumFibs(1000) should return 1785. sumFibs(4000000) should return 4613732. sumFibs(4) should return 5. sumFibs(75024) should return 60696. sumFibs(75025) should return 135721. |

**Some helpful links:**

What is the Fibonacci Sequence?

**Starter Code:**

1 2 3 4 5 |
function sumFibs(num) { return num; } sumFibs(4); |

**What my solution should achieve:**

- Return the sum of all odd Fibonacci numbers up to and including the passed number if it is a Fibonacci number.

Let’s first take a look at what the Fibonacci Sequence is all about:

The Fibonacci Sequence a series of numbers in which each number ( *Fibonacci number* ) is the sum of the two preceding numbers. It starts with 0 and 1, and the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. Based on this information and the provided starter code, I can create the following:

1 2 3 4 5 6 |
function sumFibs(num) { var a = 0, b = 1, f = 1, sum = 0; } |

where a and b are the two preceding numbers, f is the fibonacci number, and sum will be the sum of all the odd fibonacci numbers. Next I have to set the condition to determine whether a fibonacci number is odd, and if it is odd, add it to sum. There is no else here. I use a while loop because I don’t know exactly how long f will be <= num. In other words, I don’t necessarily know the exact number of iterations involved over num. The while loop is followed by an if statement which sets the condition for determining whether f is odd, and if f is odd, f should be added to sum:

1 2 3 4 5 6 7 8 9 |
function sumFibs(num) { var a = 0, b = 1, f = 1, sum = 0; while (f <= num) { if (f % 2 === 1) { sum += f; } |

Use of the modulo in the if statement is what determines whether f is odd. If f mod 2 results in a remainder of 1, f is odd. If f mod 2 results in a remainder of 0, then f is even. Test it out for yourself in the JS Console, or you can also test it out in the Modulo Calculator!

This is all well and good, but how was I going to get this while loop to advance? Since each fibonacci number is derived from the addition of the previous two numbers in the sequence, advancement of the while loop should start with the equation for the fibonacci number:

1 2 3 4 5 6 7 8 9 10 |
function sumFibs(num) { var a = 0, b = 1, f = 1, sum = 0; while (f <= num) { if (f % 2 === 1) { sum += f; f = a + b; } |

Once I stated the current fibonacci number from the addition of the first two numbers previous to the fibonacci number, I had to somehow advance the sequence so that a became b, and b became f. In other words, the new a was the old b, and the new b was the old f. This is equivalent to f being the sum of the previous two numbers in the sequence.

1 2 3 4 5 6 7 8 9 10 11 12 |
function sumFibs(num) { var a = 0, b = 1, f = 1, sum = 0; while (f <= num) { if (f % 2 === 1) { sum += f; f = a + b; a = b; b = f; } |

**Let’s see how this actually works:**

For the first fibonacci number, we have var a = 0, b = 1, and f = 1;

Then we have while (1 <= 4) {

if (1 % 2 === 1) {

sum += f;

Since 1 % 2 has a remainder of 1, that means that f is odd. f is added to sum.

f = 0 + 1;

a = b which means a = 1;

b = f which means that b = 1;

So f is still < 4, so we continue the loop.

f % 2 === 1 still holds true, and f is added to sum. Now sum = 2.

f = a + b which means that f = 1 + 1;

a = b which means that a = 1;

b = f which means that b = 2.

f is still < 4, so the while loop continues.

f % 2 === 1 does not coerce to true this time. Now f is 2, so 2 % 2 === 0. This means that f is even, and it does not get added to sum.

f = a + b which means that f = 1 + 2.

a = b which means that a = 2.

b = f which means that b = 3.

f is still < 4, so the loop continues.

f % 2 === 1 is true, so f is added to sum. Now sum = 5.

f = a + b which means that f = 2 + 3.

a = b which means that a = 3.

b = f which means that b = 5.

f is now > 4, so the while loop stops.

But at this point sum has to be returned so that the body of the sumFibs() function can be accessed outside of its local scope when invoked. sum is returned at the end of the sumFibs() function right before its closing curly bracket.

**Solution:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function sumFibs(num) { "use strict"; var a = 0, b = 1, f = 1, sum = 0; while (f <= num) { if (f % 2 === 1) sum += f; f = a + b; a = b; b = f; } return sum; } sumFibs(4); |

**Solution in ES6:**

1 2 3 4 5 6 7 8 9 10 11 12 13 |
function sumFibs(num) { let a = 0, b = 1, f = 1, sum = 0; while (f <= num) { if (f % 2 === 1) sum += f; f = a + b; a = b; b = f; } return sum; } sumFibs(4); |

This was first published here on **Jul 4, 2016 @ 01:48**. It was part of the freecodecamp.org’s **Intermediate JavaScript Algorithm Scripting** for the **Front End Development** certification.

Comment Policy:Your words are your own, so be nice and helpful if you can. Please, only use your real name and limit the amount of links submitted in your comment. We accept clean XHTML in comments, but don't overdo it please. Comments are moderated, so spammy or malicious links will be removed.