This is another interesting program which is very useful for school and college students. This program sometimes asked in Interviews for Fresher, so be prepare yourself for the future.
If you don’t know what is Fibonacci series, here is the small example,
0, 1, 1, 2, 3, 5, 8…
0,1 -> 0 +1 =1 ( 1 is the next number)
0,1,2 -> 1+1 = 2 (2 is the next number)
0,1,2 -> 1+2 = 3
0,1,2,3 -> 2+3 = 5
So basically Fibonacci series is finding the next number by adding the first two numbers
Here is the JavaScript function to find the Fibonacci series function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
function fibonacciSeries(limit) { var first_num = 0, second_num = 1, next_num, outputElement; if(limit != '') { outputElement = document.getElementById("output"); outputElement.innerHTML = ""; outputElement.innerHTML = "<br>"; outputElement.innerHTML += first_num + "<br>"; outputElement.innerHTML += second_num + "<br>"; /* here we are checking > 2 because we have already printed two numbers of fiboncacci series above */ //loop through the limit till it becomes 0 (zero) while(limit > 2) { //add the first value and second to get the next value next_num = first_num + second_num; //assign second value to first //next value to second //to find the next series first_num = second_num; second_num = next_num; //print the next value outputElement.innerHTML += next_num + "<br>"; //decrement the limit by one limit--; } }else{ document.getElementById("output").innerHTML = "Please enter a value!"; } } |
The above program is self explanatory, read the comments in the script. This function accepts limit as a parameter,
fibonacciSeries(10) -> this will print 10 values of Fibonacci series
Here is the usage example of the above fibonacci function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<html> <head> <title>Fibonacci series in pure JavaScript - Tutorialsmade.com</title> <script> //fibonacci series in pure JavaScript //by tutorialsmade.com function getFibonacci() { //get the limit from textbox var limit = document.getElementById("limit").value; //call the fibonacci function fibonacciSeries(limit); } function fibonacciSeries(limit) { var first_num = 0, second_num = 1, next_num, outputElement; if(limit != '') { outputElement = document.getElementById("output"); outputElement.innerHTML = ""; outputElement.innerHTML = "<br>"; outputElement.innerHTML += first_num + "<br>"; outputElement.innerHTML += second_num + "<br>"; /* here we are checking > 2 because we have already printed two numbers of fiboncacci series above */ //loop through the limit till it becomes 0 (zero) while(limit > 2) { //add the first value and second to get the next value next_num = first_num + second_num; //assign second value to first //next value to second //to find the next series first_num = second_num; second_num = next_num; //print the next value outputElement.innerHTML += next_num + "<br>"; //decrement the limit by one limit--; } }else{ document.getElementById("output").innerHTML = "Please enter a value!"; } } </script> </head> <body> <h2>Fibonacci series in pure JavaScript - Tutorialsmade.com</h2> <h3>Enter the limit:</h3> <input type="text" id="limit" /> <button onclick="getFibonacci()" >Print Fibonacci Series</button> <h4>Output: <span id="output"></span></h4> </body> </html> |
On click of the button, getFibonacci() function will be called, then fibonacciSeries(limit) will be called by getFibonacci() by sending the limit value from text box.
Here is the live Demo of JavaScript Fibonacci series: