Written by Malappa Kuruva on August 26, 2014 in JavaScript

Dynamically generating Fibonacci series using JavaScript

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Did you guys remember coding/programming/developing the Fibonacci series program when you first started learning C or VB.net. It is one of the most basic logic that you need to be aware of and frankly speaking I have never used this Fibonacci number generation in my professional career till now. But still developing the Fibonacci series is kind of challenge for the beginners.

We’ve started focusing on JavaScript and in this post, we are going to show you how to generate Fibonacci series dynamically by using the JavaScript. We have written few programs on Fibonacci series using Java programming language and I hope they might also come handy for you.

So, what are we going to do in this tutorial and what will our code do. Initially just go through the code once and try to understand it. When you open the code, the basic thing happens in the browser is you’ll see an alert box asking you to enter the value in order to generate the Fibonacci series till that value you’ve entered. This is one of the most flexible ways of generating the Fibonacci series in Javascript.

Download Source Code

P.S. – Please make sure you are not using the Ad-blocker add-ons in your browser.


<html>
<body>
<script type="text/javascript">
var a=0,b=1,c;
var inputNumber=prompt("Please enter fibonacci range number");
document.write("Fibonacci");
if(inputNumber!=undefined && !isNaN(inputNumber)){

while (b<=parseInt(inputNumber))
{
if(c!=undefined)
document.write(c);
document.write("<br/>");
c=a+b;
a=b;
b=c;
}
}
</script>
</body>
</html>

Output:

fibonacci series in javascript

 

fibonacci series output in javascript

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: