2018-10-12 15:37:13 -04:00
---
title: Sum All Odd Fibonacci Numbers
---
2019-07-24 00:59:27 -07:00
# Sum All Odd Fibonacci Numbers
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
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
* < a href = 'https://en.wikipedia.org/wiki/Fibonacci_number' target = '_blank' rel = 'nofollow' > Fibonacci number</ a >
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
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.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-12 15:37:13 -04:00
To check if a number is even all you have to check is if `number % 2 == 0` .
2019-07-24 00:59:27 -07:00
### Hint 3
2018-10-12 15:37:13 -04:00
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.
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
```javascript
function sumFibs(num) {
var prevNumber = 0;
var currNumber = 1;
var result = 0;
while (currNumber < = num) {
if (currNumber % 2 !== 0) {
result += currNumber;
2018-10-12 15:37:13 -04:00
}
2019-07-24 00:59:27 -07:00
currNumber += prevNumber;
prevNumber = currNumber - prevNumber;
}
return result;
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// test here
sumFibs(4);
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
* 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.
2019-06-01 07:06:54 -07:00
* We use the modulo operand to check if the current number is odd or even. If it is odd, add it to the result.
2018-10-12 15:37:13 -04:00
* Complete the Fibonacci circle by rotating getting the next number and swapping values after.
* Return the result.
#### Relevant Links
2019-05-03 14:21:09 +05:30
* < a href = 'https://www.freecodecamp.org/forum/t/javascript-while-loop/14668' target = '_blank' rel = 'nofollow' > JS while Loop</ a >
2019-07-24 00:59:27 -07:00
< / details >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 2 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
```javascript
function sumFibs(num) {
// Perform checks for the validity of the input
if (num < 0 ) return -1 ;
if (num === 0 || num === 1) return 1;
2019-04-12 21:29:47 +09:00
2019-07-24 00:59:27 -07:00
// Create an array of fib numbers till num
const arrFib = [1, 1];
let nextFib = 0;
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// 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);
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// 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);
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// test here
sumFibs(4);
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
* Create an array of fibonacci numbers till **num** .
2019-04-12 21:29:47 +09:00
* Use `filter()` method to filter out even numbers.
* Use `reduce()` method to sum the remaining (odd) values.
2018-10-12 15:37:13 -04:00
* Return the sum.
#### Relevant Links
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-push/14298' target = '_blank' rel = 'nofollow' > JS Array Prototype Push</ a >
2019-05-03 14:21:09 +05:30
* < a href = 'https://www.freecodecamp.org/forum/t/javascript-for-loops-explained/14670' target = '_blank' rel = 'nofollow' > JS For Loops Explained</ a >
2019-04-12 21:29:47 +09:00
* < a href = 'https://www.freecodecamp.org/forum/t/javascript-array-prototype-filter/14289' target = '_blank' rel = 'nofollow' > JS Array Prototype Filter</ a >
2018-10-12 15:37:13 -04:00
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-reduce/14299' target = '_blank' rel = 'nofollow' > JS Array Prototype Reduce</ a >
2019-07-24 00:59:27 -07:00
< / details >