Blog

In JavaScript Fibonacci Number Recursive Function Examples
Posted on June 16, 2015 in Algorithms, JavaScript by Matt Jennings

Basic Fibonacci Number examples with 4 as an input is below:

/*
Recursive function to return a single Fibonacci Number
*/

function fibonacciRecursive(num) {
  if(num === 0) {
    return 0;
  }
  else if(num === 1 || num === 2) {
    return 1;
  }
  else {
    return fibonacciRecursive(num - 1) + fibonacciRecursive(num - 2);
  }
  
}

// Output for line below is 3:
console.log(fibonacciRecursive(4));

Leave a Reply