Introduction
This is a coding challenge that requires finding the sum of all odd Fibonacci numbers up to a specified number. We will be given a number, and we need to return the sum of all odd Fibonacci numbers that are less than or equal to that number. This challenge consists of three parts: the first is Sum, the second is Odd, and finally, Fibonacci Numbers. We will discuss each part in detail for your better understanding.
This challenge has three steps: generate Fibonacci numbers , filter odd values, then sum them. We’ll follow that sequence. This will give us an array of Fibonacci Numbers for the given number. In the next step, we need to find all the Odd numbers that are less than or equal to the given number from the Fibonacci Numbers array, and finally, we willsum all the Odd numbers for the final output.
Fibonacci numbers are a specific series of numbers where each number is the sum of the two preceding numbers. For example, if we consider the number 10, the Fibonacci series would be as follows: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55). It starts from 0 instead of 1.
We will identify all the Fibonacci numbers less than or equal to the given number, which is 10. To do this, we first list the Fibonacci numbers: (0, 1, 1, 2, 3, 5, 8). Now, we need to find all the odd numbers from this list. Odd numbers are those that cannot be evenly divided by 2.
Here are the numbers from the Fibonacci series that are less than or equal to 10: (0, 1, 1, 2, 3, 5, 8). Next, we filter this list to find only the odd numbers: (1, 1, 3, 5). Finally, we need to sum these odd numbers: 1 + 1 + 3 + 5 = 10.
Examples
let num = 10;
Step 1: We need to find all the Fibonacci Numbers of the given number, that is 10, as you showed below:
0 = 0
1 = 1
2 = 0 + 1 = 1
3 = 1 + 1 = 2
4 = 1 + 2 = 3
5 = 2 + 3 = 5
6 = 3 + 5 = 8
7 = 5 + 8 = 13
8 = 8 + 13 = 21
9 = 13 + 21 = 34
10 = 21 + 34 = 55
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Step 2: From the list of Fibonacci numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55), we need to find the numbers that are less than or equal to the given number (10). The relevant numbers are:
0, 1, 1, 2, 3, 5, 8
Step 3: Next, we need to extract the odd numbers from this list:
1, 1, 3, 5
Step 4: Finally, we sum these odd numbers:
1 + 1 + 3 + 5 = 10
Final Solution: The sum of the odd Fibonacci numbers less than or equal to given number(10) is 10.
Solution
function sumFibs(num) {
if (num === 0) return 0;
if (num === 1) return 1;
let f1 = 0;
let f2 = 1;
let previousTotal = 0;
let fibonacciNumArr = [];
let finalResult = 0;
for (let i = 2; i <= num; i++) {
previousTotal = f1 + f2;
f1 = f2;
fibonacciNumArr.push(f2 = previousTotal);
}
let oddFibonacciNumArr = [];
for (let i = 0; i <= fibonacciNumArr.length; i++) {
if (fibonacciNumArr[i] <= num && fibonacciNumArr[i] % 2 !== 0) {
oddFibonacciNumArr.push(fibonacciNumArr[i])
}
}
finalResult = oddFibonacciNumArr.reduce((a, b) => a + b)
return finalResult + 1;
}
sumFibs(1000);
Don’t get confused, it seems longer, but as I will start explaining this, it will become much easier for you.
As you can see, we have a function named sumFibs with one parameter num where number is stored. Inside the function, we need to proceed with num, our given number. To solve this problem, we took several steps to solve this challenge. So you can see below what we did in our first step:
if (num === 0) return 0;
if (num === 1 || num === 2) return 1;
We used two conditions: if the given number num is 0, then it should return 0, as there the solution of 0 is 0, and we did the same with 1 and 2 by adding the || operator in the second condition. As we know, the Fibonacci numbers of 1 and 2 are 1. This step makes sure that if the number meets these conditions, then it should simply return the value that is provided without proceeding to the next block of code.
let f1 = 0;
let f2 = 1;
let previousTotal = 0;
let fibonacciNumArr = [];
let finalResult = 0;
We dedicated our second step to declaring all the variables that we will use later in the function for storing and updating values.
f1: It will be used to strengthen the first number for finding the Fibonacci series.
f2: It will be used to strengthen the second number for finding the Fibonacci series.
previousTotal: it will be used for storing the previous two numbers’ sum.
fibonacciNumArr: It will be used for storing all the Fibonacci numbers elements of the given number.
finalResult: It will be used for returning the final sum of all Odd Fibonacci Numbers.
In third step, we used the for loop to find all the Fibonacci numbers of the given number. We started the loop from 2 because, as we know, in the Fibonacci numbers series, the first two numbers are 0 and 1 and we started from 2 so that the loop starts from 2 up to a number.
Within the loop, we start operation we used previousTotal = f1 + f2; that means the first and second number will be added into the previousTotal, and then we used f1 = f2; that means with each next step in the loop, the f1 is kept updating by f2. And finally, we used fibonacciNumArr.push(f2 = previousTotal); We used the push method to push each Fibonacci number with each step within the fibonacciNumArr variable. Inside the push method, we used f2 = previousTotal means that each f2 is kept updated by each previous sum.
for(let i = 2; i <= num; i++) {
previousTotal = f1 + f2;
f1 = f2;
fibonacciNumArr.push(f2 = previousTotal);
}
Now we will have all the Fibonacci numbers of the given number stored in the fibonacciNumArr array. Then we declared a new variable and we will store all the Odd numbers that are less than or equal to the given number that is provided.
let oddFibonacciNumArr = [];
In the next step, we started a for loop again to get the desired numbers, which are all the Odd numbers that are less than or equal to the given number. So inside the loop we initiated a if/else condition which will return the desirable value (fibonacciNumArr[i] <= num && fibonacciNumArr[i] % 2 !== 0) where first part (fibonacciNumArr[i] <= num) of the condition makes sure that only takes the number that are <= less than or equal to the given number, while the second part (fibonacciNumArr[i] % 2 !== 0) makes sure that only Odd numbers should be returned. We used the && operator between these two conditions, which makes sure these two conditions must be met. Within the condition, we used oddFibonacciNumArr.push(fibonacciNumArr[i]) that pushes all the elements in the oddFibonacciNumArr array.
for(let i = 0; i <= fibonacciNumArr.length; i++) {
if(fibonacciNumArr[i] <= num && fibonacciNumArr[i] % 2 !== 0) {
oddFibonacciNumArr.push(fibonacciNumArr[i])
}
}
Finally, we need to sum all the Odd numbers that we recently stored in oddFibonacciNumArr. We used the reduce JavaScript built-in method for this purpose, and we stored the final sum in the finalResult Variable.
finalResult = oddFibonacciNumArr.reduce((a,b) => a + b)
Finally, we return the finalResult plus 1 because we started our Fibonacci series from 0, so we need to add 1to get the correct sum of all Odd Fibonacci numbers.
return finalResult + 1;
So, this is how we can solve the Sum All Odd Fibonacci Numbers challenge in JavaScript.