JavaScript Memoization — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Memoization lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Cache a function's result
easy10 minMemoize a function and confirm it only really runs once.
- 2
Return the cached value
easy10 minConfirm the memoized function still returns the right answer.
- 3
Cache different inputs separately
easy10 minCall with two different inputs and count the real calls.
- 4
Speed up recursive Fibonacci
easy10 minMemoize a recursive Fibonacci and get a value that would be slow otherwise.
- 5
Build a key from two arguments
medium10 minMemoize a two-argument function using a combined key.
Find the bug(5)
- 1
Fix the cache never checked
easy8 minWithout checking the cache first, every call runs the real function.
- 2
Fix the cache declared inside the wrapper
easy8 minA new cache on every call means nothing is ever remembered.
- 3
Fix the two arguments sharing one key
easy8 minDifferent inputs must not overwrite each other in the cache.
- 4
Fix an impure function memoized
easy8 minMemoizing Math.random always returns the very first value forever.
- 5
Fix the result stored before computing it
easy8 minThe value must be computed before it can be cached.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace