Fibonacci series in JavaScript
This section will discuss the Fibonacci series and how we can generate the Fibonacci series in JavaScript.
Fibonacci series is a series that generates subsequent series of numbers by the addition of the two previous numbers. The first two terms of the Fibonacci series are zero and one, respectively. And the next terms are the addition of the two previous terms.
Representation of the Fibonacci series
Fn = (Fn -1) + (Fn – 2)
Fn represents the addition of the previous terms (Fn – 1) and (Fn – 2). Here Fn-1 is the first terms, and Fn-2 is the second terms of the Fibonacci series.
Example:
- First terms of the series is: 0
- Second term of the series is: 1
- Third terms of the series is: (0 + 1) = 1
- Fourth terms of the series is: (second + third) term = (1 + 1) = 2
- Fifth term of the series is: (Third + fourth) = 1 + 2 = 3
Here is generated series: 0, 1, 1, 2, 3, … Similarly, we can find the series of the next terms.
Steps to find the Fibonacci series of n numbers
Following are the steps to find the series of the Fibonacci Series:
Step 1: Declare the variables x, y, z, n, i
Step 2: Initialize the local variable x = 1, y = 1, i = 2
Step 3: Read a number from the user
Step 4: Display the value of x and y
Step 5: Repeat the process of Fibonacci series until i > n
- z = x + y
- Display the value of z
- x = y, y = z
- i = i + 1
Step 6: Stop the execution of the Fibonacci series
Get the Fibonacci series up to n terms
Let’s consider an example to get the Fibonacci series up to a desired numbers in JavaScript using for loop.
Program1.html
Output:
When we execute the above program, it displays the given image. There is a prompt box to define the Fibonacci series limits, and then click the OK button to continue.
After that, it displays the Fibonacci series that starts with 0 and 1. And the next term is the addition of its previous two terms, as shown below.
Get the Fibonacci series of the first 8 terms
Let’s consider an example to get the Fibonacci series of the first 8 terms in JavaScript using for and if statements.
Program3.html
Output:
Get the sum of the first 7 terms of the Fibonacci series
Let’s consider an example to get the sum of the Fibonacci series in JavaScript using function and for loop.
Simple2.html
Output
When the above code is executed, it displays a prompt box that takes a number to return the sum of the Fibonacci series.
Here we have entered 7 as the input to return the series sum, as shown below.
Click on the OK button.
In the above image, it returns the sum of first 7 terms is 21.
Get the Fibonacci series using Recursion Function
Let’s consider an example to get the Fibonacci series in JavaScript using recursive function.
Recursion.html
Output
In the above program, we have created the Fibonacci series using the recursion function that avoids the loop to display the series. The recursion function continuously calls the recur() function to print the series till the recur(12) is satisfied. At each iteration, the value of recur() function is decremented by 1 and store the value into the total variable.
Get the Fibonacci series in Reverse Order
Let’s consider an example to get the Fibonacci series in Reverse Order using for loop.
Reverse.html
Output
When the above code is executed, it shows a prompt box to take a number from the user.
Here we have entered 10 as the input and then click the OK button. After that, it generates the Fibonacci series of the first 10 terms in ascending order and in reverse order.