Let us see fibo series using various methods with the help of an example as mentioned below:
1. Fibonacci Series using for loop
- In the above series, we can see that every number is the sum of the previous two consecutive numbers. The very first number and second values are 0 and 1 respectively as the series starts with zero and one. The actual functioning begins from the value in the 3rd index. The value in the third position is 1 that is nothing but the sum of two previous consecutive numbers 0 and 1.
- The value in the fourth index is 2 which is the outcome of the sun of the two consecutive digits before it. The process of adding the previous two digits and adding it to the series continues until the count of values in the series becomes equal to the total count that we wanted in the series.
var pop = prompt("Enter the count of values in the series", " ");
var var1=0, var2=1;
document.write("Here is the fibonacci series : ");
var counter, sum;
for(counter=2; counter<pop; counter++)
- Whatever number the user inputs, it will be stored in the variable named pop. The variable will be storing the total number of values that the series should have. Later we have initialized the variable var1 and var 2 that store the values 0 and 1, respectively, as these are the two mandatory values in the Fibonacci series that kicks the process required to develop the series.
2. Fibonacci Series using while loop
- There are various methods of getting the Fibonacci series and in this section, we will see how the Fibonacci series can be developed using the while loop. In the below program everything is similar to that of what we have used in the above program.
- The primary difference that we have used the while loop instead of for loop. The variables are also the same and the role was given to the variables in terms of holding the values works the same way as it does in the last section.
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.
var var1 = 0, var2 = 1, var3;
document.write("Here is the Fibonacci series with 10 values : ");
document.write(var1 + " ");
var3 = var1+var2;
var1 = var2;
var2 = var3;
- This program works fine with the while loop in order to generate the list of items in the Fibonacci series. The pop variable has to accept the values from the user that will give the total count of the count of elements in the series.
- The variable var1 and var2 have been assigned with the value 0 and 1 respectively in order to input these values when the series begins.
- Afterward, the while loop has been introduced that checks if the counter is less than the value stored in the pop variable. If found positive, it will terminate the execution and will give the list else it will sustain on execution.
3. Fibonacci Series using with recursion
- In this program, the Fibonacci series has been generated using the recursion. In the last two examples, we have developed the series using the for and the while loop but in this section, we will develop the same using the function that can be called over and over in order to get the expected series.
- The way it is different from both of the programs mentioned above is, it is not taking ay value from the user using the input box but just using the function where the value can be hardcoded.
- In the application where the developed want to prevent the application from using loops for any reason can use this way to get the same functionality as the loops do.
var fseries = function (var1)
return [0, 1];
var sum = fseries(var1 - 1);
sum.push(sum[sum.length - 1] + sum[sum.length - 2]);
- The program is using the function named f series in order to produce the Fibonacci series. The number of elements that have to be in the list is stored in the val variable whose value is assigned when the function is getting called.
- In the second last line of this program, we have written series(10) which assigned 10 to the val variable while the function is being called. The outcome of this program will be the same as that of the above two applications. The list that is the outcome of this program will be having 10 elements in the list.
- When it comes to presenting the data of the series on the screen, it will print the variable var1 and var2 that gives the values of 0 and 1 to the series and then kicks the function to add the previous two consecutive numbers and add them.
- There is a for loop implemented that will ensure that the counter is kept on incrementing by one as the loop runs and meanwhile also make sure that the counter should be less than the value in pop. The process will continue till the loop terminates which will happen right after the desired series has been generated. By the end, the script tag has been closed with states that the code after this belongs to the parent file.
- There are certain things that have to be taken care of while developing the Fibonacci series and that is the approach of writing the code. This code could be shortened to offer efficiency to the application. If this has to be defined in the form of a function, one has to follow the approach accordingly to invoke the function.
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.