Skip to main content

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. 1

    Cache a function's result

    easy10 min

    Memoize a function and confirm it only really runs once.

  2. 2

    Return the cached value

    easy10 min

    Confirm the memoized function still returns the right answer.

  3. 3

    Cache different inputs separately

    easy10 min

    Call with two different inputs and count the real calls.

  4. 4

    Speed up recursive Fibonacci

    easy10 min

    Memoize a recursive Fibonacci and get a value that would be slow otherwise.

  5. 5

    Build a key from two arguments

    medium10 min

    Memoize a two-argument function using a combined key.

Find the bug(5)

  1. 1

    Fix the cache never checked

    easy8 min

    Without checking the cache first, every call runs the real function.

  2. 2

    Fix the cache declared inside the wrapper

    easy8 min

    A new cache on every call means nothing is ever remembered.

  3. 3

    Fix the two arguments sharing one key

    easy8 min

    Different inputs must not overwrite each other in the cache.

  4. 4

    Fix an impure function memoized

    easy8 min

    Memoizing Math.random always returns the very first value forever.

  5. 5

    Fix the result stored before computing it

    easy8 min

    The 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