Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Created March 14, 2017 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleshevlin/9c6ba1841835ca5327f42f274cdcfdd5 to your computer and use it in GitHub Desktop.
Save kyleshevlin/9c6ba1841835ca5327f42f274cdcfdd5 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/kawomaxiga
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
"use strict";
function memoizedFibSum(num) {
if (!this.cache) {
this.cache = {};
}
console.log(this.cache);
if (this.cache[num]) {
return this.cache[num];
}
if (num <= 1) {
this.cache[num] = num;
return num;
} else {
var sum = memoizedFibSum(num - 1) + memoizedFibSum(num - 2);
this.cache[num] = sum;
return sum;
}
}
console.log(memoizedFibSum(0));
console.log(memoizedFibSum(1));
console.log(memoizedFibSum(3));
console.log(memoizedFibSum(10));
</script>
<script id="jsbin-source-javascript" type="text/javascript">function memoizedFibSum (num) {
if (!this.cache) {
this.cache = {}
}
console.log(this.cache)
if (this.cache[num]) {
return this.cache[num]
}
if (num <= 1) {
this.cache[num] = num
return num
} else {
const sum = memoizedFibSum(num - 1) + memoizedFibSum(num - 2)
this.cache[num] = sum
return sum
}
}
console.log(memoizedFibSum(0))
console.log(memoizedFibSum(1))
console.log(memoizedFibSum(3))
console.log(memoizedFibSum(10))
</script></body>
</html>
"use strict";
function memoizedFibSum(num) {
if (!this.cache) {
this.cache = {};
}
console.log(this.cache);
if (this.cache[num]) {
return this.cache[num];
}
if (num <= 1) {
this.cache[num] = num;
return num;
} else {
var sum = memoizedFibSum(num - 1) + memoizedFibSum(num - 2);
this.cache[num] = sum;
return sum;
}
}
console.log(memoizedFibSum(0));
console.log(memoizedFibSum(1));
console.log(memoizedFibSum(3));
console.log(memoizedFibSum(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment