Follow

Follow

# Sum All Odd Fibonacci Numbers - JavaScript Solution & Walkthrough

## (13/21) Learn how to solve coding challenges using FreeCodeCamp's curriculum.

Benjamin Semah
·Apr 28, 2022· • 12/21 Sum All Odd Fibonacci Numbers
• Understanding the Challenge
• Pseudocode
• Solving the Challenge
• Final Solution
• Congratulations! ## 12/21 Sum All Odd Fibonacci Numbers

Given a positive integer `num`, return the sum of all odd Fibonacci numbers that are less than or equal to `num`.

The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.

For example, `sumFibs(10)` should return `10` because all odd Fibonacci numbers less than or equal to `10` are 1, 1, 3, and 5.

``````function sumFibs(num) {

return num;
}

sumFibs(4);
``````

Credit: FreeCodeCamp.org

## Understanding the Challenge

In this, you are expected to create a function called `sumFibs()`.

The function takes a positive integer `num` as input. It then returns the sum of all odd Fibonacci numbers that are less than or equal to `num`.

To get a better understanding of this challenge, you need first understand the Fibonacci sequence.

We are told in the challenge description that "the first two numbers in the Fibonacci sequence are 1 and 1." After these first two numbers, any number that comes next is the sum of the previous two numbers.

So the first six numbers in the Fibonacci sequence is as follows; 1, 1, 2, 3, 5, 8.

Third number is 2 because 1 + 1 = 2. Fourth number is 3 because 1 + 2 = 3. Fifth number is 5 because 2 + 3 = 5. Sixth number is 8 because 3 + 5 = 8.

You get the drill right?

So moving on, here's what the `sumFibs()` function in expected to do. Take an input `num` and return the sum of all odd numbers in the Fibonacci sequence less than or equal to `num`.

For example `sumFibs(10)` should return `10`

This is because the numbers in the Fibonacci less than or equal to 10 are 1, 1, 2, 3, 5, and 8. Of these, the ones that are odd numbers are 1, 1, 3 and 5. Therefore, 1+1+3+5 equals 10.

## Pseudocode

``````Given an integer (num)
find all numbers in Fibonacci sequence less than or equal to num
store them in an array
filter array to get only the odd numbers
add all the odd numbers in the filtered array
return the sum
``````

## Solving the Challenge

To begin, we need an array to store all numbers in the Fibonacci sequence less than or equal to `num`. Let's call the array `fibArray`. We're informed in the challenge description that the first two numbers in the Fibonacci sequence are 1 and 1.

``````const fibArray = [1, 1]
``````

With our knowledge of the first two numbers, we can calculate what the subsequent numbers are. Every next number is a sum of the two previous numbers. However, we only need odd Fibonacci numbers that are less than or equal to `num`.

For the numbers to be summed up, two conditions must be met. First, the numbers should be less than or equal to `num` and they should also be odd numbers.

To get numbers that meet these two conditions, we are going to use a `for loop` initialized `i = 1` and ends at `i <= num`.

``````  for (let i = 1; i <= num; i++) {
const prevNum1 = fibArray[fibArray.length-1]
const prevNum2 = fibArray[fibArray.length-2]
const fibNumber = prevNum1 + prevNum2

if (fibNumber <= num) {
fibArray.push(fibNumber)
}
}
``````

To get the next number in the Fibonacci sequence `fibNumber`, we add the previous two numbers `prevNum1` and `prevNum2`. Then, we check if `fibNumber` is less than `num`. If it is, we push it to the `fibArray`.

At this point, the `fibArray` contains all numbers in the Fibonacci sequence less than `num` (which is 4 in the case of the example we are working with).

``````console.log(fibArray) // [1, 1, 2, 3]
``````

As you can see, not all of them are odd numbers. We need a filter to get only the odd numbers.

``````  const fibOddNumbers = fibArray.filter(num => {
return num % 2 == 1
})
``````

Now, when we log `fibOddNumbers`, we will get an array of all odd numbers in the Fibonacci sequence less than 4.

``````console.log(fibOddNumbers) // [1, 1, 3]
``````

Now, we can sum up all the numbers in the `fibOddNumbers` array. Let's save it in a variable called `sum`.

``````  let sum = 0

fibOddNumbers.forEach(oddNum => {
sum += oddNum
})
``````

Now, all that's left to be done is to return `sum` and our function is complete!

``````return sum
``````

## Final Solution

``````function sumFibs(num) {
const fibArray = [1, 1]

for (let i = 1; i <= num; i++) {
const prevNum1 = fibArray[fibArray.length-1]
const prevNum2 = fibArray[fibArray.length-2]
const fibNumber = prevNum1 + prevNum2

if (fibNumber <= num) {
fibArray.push(fibNumber)
}
}

const fibOddNumbers = fibArray.filter(num => {
return num % 2 == 1
})

let sum = 0

fibOddNumbers.forEach(oddNum => {
sum += oddNum
})

return sum;
}

sumFibs(4); // 5
``````

## Congratulations!

You just cracked the 12th challenge in this series. Cheers and happy coding!