Fibonacci Series In JavaScript
Posted by Superadmin on May 02 2023 13:37:40

Fibonacci Series In JavaScript

By Priya PedamkarPriya Pedamkar
  

Fibonacci Series In JavaScript

Introduction to Fibonacci Series in JavaScript

The following article will help us how to find the Fibonacci Series in JavaScript. The functionality that we enjoy in the web applications is provided by the programming languages that operate on a server but that’s not all. The user interface of the application is something that helps the user to interact with the web application and hence considered equally important when it comes to designing a Web application. In this article, we are going to learn about one of the client-side scripting languages that endorse UI designing, known as JavaScript. JavaScript enables the application to dynamically create the populate the web page components. Working on JavaScript needs logics to be used in order to bring particular functionalities. Here we will see how the Fibonacci Series is written in JavaScript.

Fibonacci Series of JavaScript Using various Methods

Let us see fibo series using various methods with the help of an example as mentioned below:

Fibonacci Series of JavaScript Using various Methods

1. Fibonacci Series using for loop

Program

<script type="text/javascript">
var pop = prompt("Enter the count of values in the series", " ");
var var1=0, var2=1;
document.write("Here is the fibonacci series : ");
document.write("",var1," ");
document.write("",var2," ");
var counter, sum;
for(counter=2; counter<pop; counter++)
{
sum=var1+var2;
document.write("",sum," ");
var1=var2;
var2=var3;
}
</script>

Output:

fibnonacci series in javascript

2. Fibonacci Series using while loop

So as the outcome, the output of this program will also be the same as what we get after executing the last for loop code.

Program

<script type="text/javascript">
var var1 = 0, var2 = 1, var3;
document.write("Here is the Fibonacci series with 10 values : ");
while(var1<40)
{
document.write(var1 + " ");
var3 = var1+var2;
var1 = var2;
var2 = var3;
}
</script>

Output:

with 10 values

3. Fibonacci Series using with recursion

Program

<script>
var fseries = function (var1)
{
if (var1===1)
{
return [0, 1];
}
else
{
var sum = fseries(var1 - 1);
sum.push(sum[sum.length - 1] + sum[sum.length - 2]);
return sum;
}
};
document.write(fseries(10));
</script>

Output:

Series in 10 values

Conclusion

The modern web application needs various functionalities in the frontend as well as in the backend and the Fibonacci series is a method that could be used to offer particular functionality to the application. It is also used by the students to develop the logic to write an application and can be helpful in various manners.