Created
March 14, 2017 16:44
-
-
Save kyleshevlin/9c6ba1841835ca5327f42f274cdcfdd5 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/kawomaxiga
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
<!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> |
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
"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