Created
November 4, 2015 02:36
-
-
Save st32lthx/6943bd19a023e4b4b303 to your computer and use it in GitHub Desktop.
Simple Memoization Example in JavaScriopt
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
var fibo = (function () { | |
var memo = {}; | |
function fi(n) { | |
if (n < 0) { return -1 } else { | |
var value = (n in memo) ? memo[n] : (!n || n === 1) ? 1 : fi(n - 1) + fi(n - 2); | |
memo[n] = value; | |
return value; | |
} | |
} | |
return fi; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment