# freeCodeCamp Challenge Guide: Sum All Odd Fibonacci Numbers

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 11:27pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084 "2016-06-30T23:27:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 11:27pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/1 "2016-06-30T23:27:26Z")

</div>

# [Sum All Odd Fibonacci Numbers](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-odd-fibonacci-numbers)

* * *

## Problem Explanation

You will need to gather all the **Fibonacci** numbers and then check for the odd ones. Once you get the odd ones then you will add them all. The last number should be the number given as a parameter if it actually happens to be an off Fibonacci number.

#### Relevant Links

- [Fibonacci number](https://en.wikipedia.org/wiki/Fibonacci_number)

* * *

## Hints

### Hint 1

To get the next number of the series, you need to add the current one to the previous and that will give you the next one.

### Hint 2

To check if a number is even all you have to check is if `number % 2 == 0`.

### Hint 3

As you get the next odd one, don’t forget to add it to a global variable that can be returned at the end. `result += currNumber;` will do the trick.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```javascript
> function sumFibs(num) {
> let prevNumber = 0;
> let currNumber = 1;
> let result = 0;
> while (currNumber <= num) {
> if (currNumber % 2 !== 0) {
> result += currNumber;
> }
> currNumber += prevNumber;
> prevNumber = currNumber - prevNumber;
> }
> 
> return result;
> }
> 
> // test here
> sumFibs(4);
> 
> ```
> 
> #### Code Explanation
> 
> - Create a variable to keep record of the current and previous numbers along with the result that will be returned.
> - Use a while loop to make sure we do not go over the number given as parameter.
> - We use the modulo operand to check if the current number is odd or even. If it is odd, add it to the result.
> - Complete the Fibonacci circle by rotating getting the next number and swapping values after.
> - Return the result.
> 
> #### Relevant Links
> 
> - [JS while Loop](http://forum.freecodecamp.org/t/javascript-while-loop/14668)

> **Solution 2 (Click to Show/Hide)**
>
> ```javascript
> function sumFibs(num) {
> // Perform checks for the validity of the input
> if (num <= 0) return 0;
> 
> // Create an array of fib numbers till num
> const arrFib = [1, 1];
> let nextFib = 0;
> 
> // We put the new Fibonacci numbers to the front so we
> // don't need to calculate the length of the array on each
> // iteration
> while ((nextFib = arrFib[0] + arrFib[1]) <= num) {
> arrFib.unshift(nextFib);
> }
> 
> // We filter the array to get the odd numbers and reduce them to get their sum.
> return arrFib.filter(x => x % 2 != 0).reduce((a, b) => a + b);
> }
> 
> // test here
> sumFibs(4);
> 
> ```
> 
> #### Code Explanation
> 
> - Create an array of fibonacci numbers till **num**.
> - Use `filter()` method to filter out even numbers.
> - Use `reduce()` method to sum the remaining (odd) values.
> - Return the sum.
> 
> Note that this solution will be slower than Solution 1, as dynamically creating an array is rather slow, especially in JavaScript.
> 
> #### Relevant Links
> 
> - [JS Array Prototype Push](http://forum.freecodecamp.com/t/javascript-array-prototype-push/14298)
> - [JS For Loops Explained](http://forum.freecodecamp.org/t/javascript-for-loops-explained/14670)
> - [JS Array Prototype Filter](http://forum.freecodecamp.org/t/javascript-array-prototype-filter/14289)
> - [JS Array Prototype Reduce](http://forum.freecodecamp.com/t/javascript-array-prototype-reduce/14299)

> **Solution 3 (Click to Show/Hide)**
>
> ```javascript
> function sumFibs(num) {
> // Every third Fibbonaci number is even
> // and the rest are odd
> let f0 = 0;
> let f1 = 1;
> let f2 = 1;
> 
> // Generate triples until num is reached
> let sum = 0;
> while (f1 <= num) {
> // Update sum
> sum += f1;
> if (f2 <= num) sum += f2;
> 
> // Compute next three Fibonacci numbers
> [f0, f1] = [f1 + f2, f2 + (f1 + f2)];
> f2 = f0 + f1;
> }
> 
> return sum;
> }
> 
> ```
> 
> ## Code Explanation
> 
> ### General idea
> 
> It is a property of Fibonacci numbers that every third number in the sequence is even and the rest are odd.
> 
> ### Algorithm
> 
> - Use three work variables to hold the the current 3 Fibonacci numbers
> - Generate triples as long as the first odd value is less than `num`
> - Add the two odd values, `f1` and `f2`, to the running `sum` of odd Fibonacci numbers

---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 10, 2016, 2:49am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/2 "2016-08-10T02:49:55Z")

</div>

Closed on August 10, 2016, 2:49am UTC



---

<div class="post-metadata">

**Author:** ![Rafase282](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rafase282/32/161583_2.png) [@Rafase282](https://forum.freecodecamp.org/u/Rafase282)\
**Post date:** [August 20, 2016, 3:56am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/3 "2016-08-20T03:56:23Z")

</div>

Opened on August 20, 2016, 3:56am UTC



---

<div class="post-metadata">

**Author:** ![weebuzer](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@weebuzer](https://forum.freecodecamp.org/u/weebuzer)\
**Post date:** [September 20, 2016, 9:07am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/4 "2016-09-20T09:07:06Z")

</div>

my code keep on freezing browser.

---

<div class="post-metadata">

**Author:** ![QuincyLarson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/quincylarson/32/212400_2.png) [@QuincyLarson](https://forum.freecodecamp.org/u/QuincyLarson)\
**Post date:** [September 25, 2016, 12:00am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/5 "2016-09-25T00:00:16Z")

</div>

It sounds like you have accidentally created an infinite loop. You’ll need to manually fix your browser by deleting the solution from your browser’s local storage: [http://forum.freecodecamp.com/t/clear-specific-values-from-your-browser-local-storage/19128](http://forum.freecodecamp.com/t/clear-specific-values-from-your-browser-local-storage/19128)

---

<div class="post-metadata">

**Author:** ![weebuzer](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@weebuzer](https://forum.freecodecamp.org/u/weebuzer)\
**Post date:** [September 26, 2016, 7:01am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/6 "2016-09-26T07:01:48Z")

</div>

I followed the instruction, but i still have issues: here is a code that i was just trying to test

function sumFibs(num) {  
var fibs=[];  
for(var i=0 ; i\< num ;i++){  
fibs.push(i);  
}

return fibs;  
}

sumFibs(4);

is there anything wrong with that code? I just don’t understand why the code didn’t stop when i=4

---

<div class="post-metadata">

**Author:** ![weebuzer](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@weebuzer](https://forum.freecodecamp.org/u/weebuzer)\
**Post date:** [September 27, 2016, 7:16am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/8 "2016-09-27T07:16:16Z")

</div>

I know what you meant, my problem is that when i run that my browser freeze, and i just could not understand why.

---

<div class="post-metadata">

**Author:** ![weebuzer](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@weebuzer](https://forum.freecodecamp.org/u/weebuzer)\
**Post date:** [September 28, 2016, 8:11am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/10 "2016-09-28T08:11:53Z")

</div>

Good to know that. At least now I know it works on your console. There has to be something wrong with my browser. Thanks

---

<div class="post-metadata">

**Author:** ![weebuzer](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@weebuzer](https://forum.freecodecamp.org/u/weebuzer)\
**Post date:** [September 28, 2016, 8:26am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/11 "2016-09-28T08:26:27Z")

</div>

Is there any online IDE that i could test this out?

---

<div class="post-metadata">

**Author:** ![weebuzer](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@weebuzer](https://forum.freecodecamp.org/u/weebuzer)\
**Post date:** [September 30, 2016, 2:39pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/13 "2016-09-30T14:39:03Z")

</div>

Oh really i didn’t know that was the case. I got really frustrated with my browser for a few days. thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![MelBrooksKA](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/melbrookska/32/73430_2.png) [@MelBrooksKA](https://forum.freecodecamp.org/u/MelBrooksKA)\
**Post date:** [October 4, 2016, 8:18am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/14 "2016-10-04T08:18:03Z")

</div>

I’m curious, I finished the problem with a solution similar to the Basic one posted above, but instead of using an IF statement to check if the value was even or odd, I used modulo and used the remainder of the number % 2 times the number to get the sum of all the odd numbers and it got me wondering which of these operations would be more efficient, assuming that the efficiency really mattered.

my code:

```auto
function sumFibs(num) {
  if(num < 0) {
    return undefined;
  }
  var sumOdds = 0; // Sum of all odd fib numbers
  for(var n1 = 0, n2 = 1, temp; n2 <= num; temp = n2, n2 += n1, n1 = temp) {
    sumOdds += n2 * (n2 % 2);
  }
  return sumOdds;
}

```

---

<div class="post-metadata">

**Author:** ![Docwali777](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/docwali777/32/65065_2.png) [@Docwali777](https://forum.freecodecamp.org/u/Docwali777)\
**Post date:** [October 5, 2016, 3:01pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/15 "2016-10-05T15:01:11Z")

</div>

Solution -

function sumFibs(num) {

var fib = [];

for (var i =0; i \< num; i++){  
if (i ===0){  
fib[i] = 1;  
}  
if (i ===1){  
fib[i] = 1;  
}  
}

for (var j =2; j \< num; j++){  
fib[j] = fib[j-2] + fib[j-1];  
}  
var sumOdd = fib.filter(function(val){  
return val%2 !==0 && val \<= num;  
});

return sumOdd.reduce(function(a, b){  
return a+b;  
});

}

sumFibs(1000);

---

<div class="post-metadata">

**Author:** ![fotonist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fotonist/32/24495_2.png) [@fotonist](https://forum.freecodecamp.org/u/fotonist)\
**Post date:** [October 18, 2016, 3:50pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/16 "2016-10-18T15:50:24Z")

</div>

use this:  
https://repl.it/languages?lite=true

---

<div class="post-metadata">

**Author:** ![drguildo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/drguildo/32/161905_2.png) [@drguildo](https://forum.freecodecamp.org/u/drguildo)\
**Post date:** [December 12, 2016, 10:40am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/17 "2016-12-12T10:40:31Z")

</div>

```auto
function sumFibs(num) {
  var a = 1, b = 1;
  var sum = 2;
  
  var tmp = a + b;
  while (tmp <= num) {
    if ((tmp % 2 !== 0) && (tmp <= num)) {
      sum += tmp;
    }
    a = b;
    b = tmp;
    tmp = a + b;
  }
  
  return sum;
}

```

---

<div class="post-metadata">

**Author:** ![akashkriplani](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@akashkriplani](https://forum.freecodecamp.org/u/akashkriplani)\
**Post date:** [January 9, 2017, 11:14am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/18 "2017-01-09T11:14:36Z")

</div>

Here’s an alternate basic solution:

```
function sumFibs(num) {
  
  var a = 1, b = 1, sum = 0;
  
  while (a <= num && b <= num) {
     if (a%2 !== 0)
       sum+=a;
     if (b%2 !== 0)
       sum+=b;
     a = a + b;
     b = a + b;
 }
   if(a<=num && a%2 !== 0) /* this means b >=num but a<=num */
         sum += a;
  else if(b <= num && b%2 !== 0) /* this means a>=num but b<=num*/
         sum += b;
     
  
  return sum;
}

sumFibs(75025);
```

---

<div class="post-metadata">

**Author:** ![dmitry-zabolotny](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dmitry-zabolotny/32/66553_2.png) [@dmitry-zabolotny](https://forum.freecodecamp.org/u/dmitry-zabolotny)\
**Post date:** [February 4, 2017, 7:09pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/19 "2017-02-04T19:09:36Z")

</div>

> [@camperbot](#):
>
> We use the modulo operand to check if the current number is odd or even. If it is **even** , add it to the result.

There is typo in the code explanation above, please change ‘even’ to ‘odd’

---

<div class="post-metadata">

**Author:** ![MindWhys](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mindwhys/32/34187_2.png) [@MindWhys](https://forum.freecodecamp.org/u/MindWhys)\
**Post date:** [February 18, 2017, 3:00pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/20 "2017-02-18T15:00:59Z")

</div>

Basic solution with comments for clarification.

```
function sumFibs(num) {
  var prevNumber = 0; //creates variable prevNumber with value 0
  var currNumber = 1;//create variable currNumber with value 1
  var result = 0;//creates variable result with value 0
  while (currNumber <=num){ //while curr is less than or equal to the given sumFibs number...
if (currNumber % 2 !== 0){ //if the remainder when divided by two is NOT 0...
  result += currNumber; //...add that number to result
}
currNumber += prevNumber; //make currNumber equal to currNUmber plus prevNumber
prevNumber = currNumber - prevNumber;//make prevNumber equal to new currNumber minus the old prevNumber
  }
  return result; //return the result
}

sumFibs(4000000);
```

---

<div class="post-metadata">

**Author:** ![RaneWrites](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ranewrites/32/44163_2.png) [@RaneWrites](https://forum.freecodecamp.org/u/RaneWrites)\
**Post date:** [March 19, 2017, 3:16am UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/21 "2017-03-19T03:16:16Z")

</div>

```
function sumFibs(num) {
  var mySequence = [1, 1];
  var sequenceOfOdds = [1, 1];
  var i = 1;
  
  do {
    i = mySequence[mySequence.length-1] + mySequence[mySequence.length-2];
    if (i <= num) {
      mySequence.push(i);
      if (i % 2 !== 0) sequenceOfOdds.push(i);
    }
  } while(i < num);
  

  return sequenceOfOdds.reduce(function(a, b) { return a+b; });
}

sumFibs(4);
```

---

<div class="post-metadata">

**Author:** ![aosdab](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@aosdab](https://forum.freecodecamp.org/u/aosdab)\
**Post date:** [March 23, 2017, 8:58pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/22 "2017-03-23T20:58:35Z")

</div>

```
function sumFibs(num) {
  
  var fiboArray = [1, 1, 2]; 
  //Initialize array with #s to avoid infinite loop
  var fibo = 0;
  var currentVal = 2; //Set sum of first 2 odd numbers
  
  for (var i = 2; fiboArray[i] <= num; i++) {
    //Loop through array, conditional stop is current fibonacci # less than or equal to given value
    fibo = fiboArray[i] + fiboArray[i - 1];
    //Add next fibonacci # to array
    fiboArray.push(fibo);
    
    if (fiboArray[i] % 2 === 1) { 
      //If current fibonacci number is odd, add it to sum
      currentVal += fiboArray[i];
    }
  }

  return currentVal;
}

```

I wanted to use an array to keep track of my Fibonacci sequence, but I had a hard time getting past the infinite loop created by the first two numbers.

---

<div class="post-metadata">

**Author:** ![curiousNoob](https://avatars.discourse-cdn.com/v4/letter/c/58f4c7/32.png) [@curiousNoob](https://forum.freecodecamp.org/u/curiousNoob)\
**Post date:** [March 27, 2017, 2:54pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084/23 "2017-03-27T14:54:16Z")

</div>

> [@camperbot](#):
>
> If it is even

In Basic Code Solution this should be **If it is odd** or **If it isn’t even**

[Next page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-sum-all-odd-fibonacci-numbers/16084.md?page=2)
