Skip to content

Instantly share code, notes, and snippets.

@Shariar-Hasan
Last active January 20, 2021 18:16
Show Gist options
  • Save Shariar-Hasan/58c6ec042d018c3933327ab38092ca99 to your computer and use it in GitHub Desktop.
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
// 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