Fibonacci series implementation in JavaScript

Fibonacci Series is a series in which the next number of the series is the sum of the previous 2 numbers. For example :- 0, 1, 1, 2, 3, 5, 8...
Took 3 minutes and 15 seconds to code fibonacci series using JavaScript. Here is the code snippet.


Code:
function printFibonacciSeries() {
var first_num = 1;
var second_num = 1;
document.getElementById('demo').innerHTML +=
first_num + "<br>" + second_num + "<br>";
for (var i = 0; i < 12; i++) {
third_num = first_num + second_num;
document.getElementById('demo').innerHTML += third_num + '<br>';
first_num = second_num;
second_num = third_num;
}
}

printFibonacciSeries();

Result:
1
1
2
3
5
8
13
21
34
55
89
144
233
377

No comments:

Post a Comment

GITEX Dubai Talk by Saion Gupta and Saiasmi Gupta

Good morning, everyone. I'm Saion Gupta, the Founder of 10xCoderKids and the Guinness World Record holder for the youngest computer prog...