Created
May 24, 2017 11:42
-
-
Save ronaiza-cardoso/04be0cb361c65822f6fd0d0c448767aa to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Memoization () { | |
this.memo = {} | |
} | |
Memoization.prototype.fib = n => { | |
// base cases | |
if (n === 0 || n === 1) | |
return n | |
// see if we’ve already calculated this | |
if (this.memo.hasOwnProperty(n)) | |
console.log(`grabbing memo[ ${n} ]`) | |
return this.memo[n] | |
console.log(`computing fib( ${n} )`) | |
const result = this.fib(n — 1) + this.fib(n — 2) | |
// memoize | |
this.memo[n] = result | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment