Tyler La Fronz's Portfolio

This site hosts a collection of my latest projects and descriptions of them. Please feel free to check them out and reach out to me if you have any questions.

Read more about me

Recursive Fibonacci in Javascript

Skills used and tools used: #Javascript, #Algorithms, #HTML, #Jekyll, #jQuery, and #CSS
Written by Tyler La Fronz on February 6, 2018

The Fundamentals

At its core, a basic Fibonacci algorithm is incredibly slow and inefficient. This implementation of Fibonacci includes a data structure to memorize previously calculated numbers in the sequence.

Please enter a value:

The function that computes this

var memo = [0, 1]; //Stores the previously calculated values

function fib(fibNum) {
  var result = 0;
  if (memo[fibNum]) { //Checks for previously calculated value
    result = memo[fibNum]; //returns the value stored
  } else if (fibNum >= 2) {
    result = fib(fibNum - 1) + fib(fibNum - 2); //recursively calls for the value
    memo[fibNum] = result; //Stores the result
  }
  return result; //returns the value
}