Skip to content

Instantly share code, notes, and snippets.

@ronaiza-cardoso
Created May 24, 2017 11:42
Show Gist options
  • Save ronaiza-cardoso/04be0cb361c65822f6fd0d0c448767aa to your computer and use it in GitHub Desktop.
Save ronaiza-cardoso/04be0cb361c65822f6fd0d0c448767aa to your computer and use it in GitHub Desktop.
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