Answers for "fibonacci recursive function js"

2

javascript recursive fibonacci

// Recursion way to return Fibonacci numbers
function recursiveFibonacci(n){
	// base case
	if(n === 0) return 0
  	if(n === 1) return 1
  
  	// recursioin base
  	return recursiveFibonacci(n - 2) + recursiveFibonacci(n - 1);
}
recursiveFibonacci(6) // should return 8

//////// and ////////

// Iteration way to return Fibonacci numbers
function iterativeFibonacci(n) {
  if (n === 0) return 0;
  if (n === 1) return 1;

  let previous = 0;
  let current = 1;
  for (let i = n; i > 1; i--) {
    let next = previous + current;
    previous = current;
    current = next;
  }
  return current;
}

iterativeFibonacci(6) // should return 8
Posted by: Guest on February-11-2021
0

fibonacci recursive method

let fibonacci = function(result, len) {
  // result[0, 1]; considering result as an array
  if(result.length >= len) {return result;} // exit recursion when limit reached.
  result.push(result[result.length-2] + result[result.length-1]);
  return fibonacci(result, len);  //return function until limit is reach.
}
Posted by: Guest on April-25-2021

Code answers related to "fibonacci recursive function js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language