Answers for "fibonacci using recursion javascript"

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
5

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);
Posted by: Guest on December-14-2020
1

fibonacci recursion javascript

function fibonacci(n) {
    if (n < 2){
        return 1;
    }else{
        return fibonacci(n-2) + fibonacci(n-1);
    }
}

console.log(fibonacci(7));
//Returns 21
Posted by: Guest on June-05-2021
1

js fibonacci sequence

var i;
    var fib = []; // Initialize array!

    fib[0] = 0;
    fib[1] = 1;
    for (i = 2; i <= 10; i++) {
      // Next fibonacci number = previous + one before previous
      // Translated to JavaScript:
      fib[i] = fib[i - 2] + fib[i - 1];
      console.log(fib[i]);
    }
Posted by: Guest on July-03-2020

Code answers related to "fibonacci using recursion javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language