Last active
January 20, 2021 18:16
-
-
Save Shariar-Hasan/58c6ec042d018c3933327ab38092ca99 to your computer and use it in GitHub Desktop.
Print n-th value of fibonaci series or Print the whole fibonacci series til n-th value using loop 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
// if you want the whole series of fibonacci | |
var fib = [0, 1]; | |
var n = 10; // you can change this value | |
if (n == 1 || n == 2) { | |
console.log(fib); | |
} | |
else { | |
for (var i = 2; i < n; i++) { | |
fib.push(fib[i-1]+fib[i-2]); | |
} | |
console.log(fib); | |
} | |
// if you want only the nth value | |
var fib = [0, 1]; | |
var n = 10; // you can change this value | |
if (n == 1) { | |
console.log(fib[0]); | |
} | |
else if(n == 2){ | |
console.log(fib[1]) | |
} | |
else { | |
for (var i = 2; i < n; i++) { | |
fib.push(fib[i-1]+fib[i-2]); | |
} | |
console.log(fib[n-1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment