Closures & Scope

Memoization

Memoization

Memoization uses a closure to remember previous inputs and results. The wrapped function still looks like a normal function to callers, but it can skip work when it has already seen the same input.

The cache must live outside the returned function's individual calls, but inside the wrapper so it stays private. Repeated calls with the same argument should return the cached result without calling the original function again.

Your task: Complete memoizeOneArg. It receives a single-argument function and returns a cached version of it. The cached function should store results by argument value and reuse them on repeated calls.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Memoization

Memoization uses a closure to remember previous inputs and results. The wrapped function still looks like a normal function to callers, but it can skip work when it has already seen the same input.

The cache must live outside the returned function's individual calls, but inside the wrapper so it stays private. Repeated calls with the same argument should return the cached result without calling the original function again.

Your task: Complete memoizeOneArg. It receives a single-argument function and returns a cached version of it. The cached function should store results by argument value and reuse them on repeated calls.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.