Last active
January 29, 2016 18:24
-
-
Save zspencer/91e3c06ce855e869c582 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findFib(index) { | |
var values = [0,1]; | |
if(index in values) { return values[index]; } | |
return findFib(index - 2) + findFib(index - 1); | |
} | |
function memoize(fn) { | |
var cache = {}; | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
if(!(args in cache)) { cache[args] = fn.apply(this, args); } | |
return cache[args]; | |
}; | |
} | |
findFib = memoize(findFib); | |
console.log(findFib(35)); | |
console.log(findFib(35)); | |
console.log(findFib(35)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment