Generating N Fibonacci Series using Javascript code example
Example 1: how to generate a fibonacci sequence in javascript
let fibonacci = [0,1];
function listFibonacci(num) {
for (let i = 1; i < num; i++) {
fibonacci.push(fibonacci[i] + fibonacci[i - 1]);
}
console.log(fibonacci);
}
listFibonacci(10);
Example 2: recursive function for fibonacci series in java javascript
var fib = function(n) {
if (n === 1) {
return [0, 1];
} else {
var arr = fib(n - 1);
arr.push(arr[arr.length - 1] + arr[arr.length - 2]);
return arr;
}
};
console.log(fib(8));