generate fibonacci series in 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: fibonacci javascript
function Fibonacci(num){
var before = 0;
var actual = 1;
var next = 1;
for(let i = 0; i < num; i++){
console.log(next)
before = actual + next;
actual = next
next = before
}
}
Fibonacci(100);
Example 3: fibonacci javascript
function fibonacci(num){
var num1=0;
var num2=1;
var sum;
var i=0;
for (i = 0; i < num; i++){
sum=num1+num2;
num1=num2;
num2=sum;
}
return num2;
}
Example 4: javascript fibonacci series
const fibo = (n, memo = {}) => memo[n] || (n <= 2 ? 1 : (memo[n] = fibo(n - 1, memo) + fibo(n - 2, memo)));
fibo(6);