Last active
March 27, 2018 10:35
-
-
Save msaxena25/312430374a031f2022ac3f2e0f560822 to your computer and use it in GitHub Desktop.
Fibonacci Series in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function printFib(count) { | |
var n1 = 0; // first char is 0 of fibonacci series | |
var n2 = 1; // seconds char is also fixed '1' | |
var output = ""; | |
output = n1 + " " + n2; | |
// loop start from 2 because we know the first and second chars | |
for (var i = 2; i < count; i++) { | |
var n3 = n2 + n1; // sum of previous two element | |
output += " " + n3; | |
n1 = n2; | |
n2 = n3; | |
} | |
console.log(output); | |
} | |
printFib(10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var n1 = 0; // first char is 0 of fibonacci series | |
var n2 = 1; // seconds char is also fixed '1' | |
var output = ""; | |
output = n1 + " " + n2; | |
function printFib1(count) { | |
if (count > 2) { | |
var n3 = n2 + n1; // sum of previous two element | |
output += " " + n3; | |
n1 = n2; | |
n2 = n3; | |
printFib1(count -1); | |
} | |
} | |
printFib1(10); | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment