Returnthe sum of all odd Fibonacci numbers up toandincluding the passed number ifit isaFibonacci 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:

JavaScript

1

2

3

4

5

6

functionsumFibs(num){

vara=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:

JavaScript

1

2

3

4

5

6

7

8

9

functionsumFibs(num){

vara=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:

PHP

1

2

3

4

5

6

7

8

9

10

functionsumFibs(num){

vara=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.

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

functionsumFibs(num){

vara=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:

Sum All Odd Fibonacci Numbers Solution

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

functionsumFibs(num){

"use strict";

vara=0,

b=1,

f=1,

sum=0;

while(f<=num){

if(f%2===1)

sum+=f;

f=a+b;

a=b;

b=f;

}

returnsum;

}

sumFibs(4);

Solution in ES6:

Sum All Fibonacci Numbers Solution in ES6

JavaScript

1

2

3

4

5

6

7

8

9

10

11

12

13

functionsumFibs(num){

leta=0,b=1,f=1,sum=0;

while(f<=num){

if(f%2===1)

sum+=f;

f=a+b;

a=b;

b=f;

}

returnsum;

}

sumFibs(4);

Share this:

Like this:

Related

Maria Campbell

Founder, Inter-Global Media Network, Inc., where I focus on Front End Development. as well as a digital photographer, videographer, blogger, broadcaster, and graphic designer. Genesis Framework user. I got my start 8+ years ago teaching myself HTML/CSS and Wordpress Development because I couldn't find anyone who could create what I wanted. I have been working with open source software ever since. I am an open source software evangelist, and a "Women in Technology" evangelist. A creative professional myself, I find Web Development to be both practical and creative. I love to cook, travel, read mysteries, love everything French, the beach at sunrise, my three cats, and hope to revisit the city of San Francisco one day.

Reader Interactions

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.