Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save msaxena25/312430374a031f2022ac3f2e0f560822 to your computer and use it in GitHub Desktop.
Save msaxena25/312430374a031f2022ac3f2e0f560822 to your computer and use it in GitHub Desktop.
Fibonacci Series in JavaScript
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);
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