javascript fibonacci series code example

Example 1: js fibonacci sequence

// number fibonnaci to array format
function fibonacci(nums) {
  
  let fib = [0, 1];
  let data = [];
  
  for(let i = 2; i <= nums; i++) {
    fib[i] = fib[i - 1] + fib[i - 2]; 
    data.push(fib[i]);
  }
  
  return data;
}

Example 2: 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 3: javascript fibonacci series

const fibo = (n, memo = {}) => memo[n] || (n <= 2 ? 1 : (memo[n] = fibo(n - 1, memo) + fibo(n - 2, memo)));

// Examples
fibo(6);    // 8