Accidental Shared State
Accidental Shared State
Closures are useful because they keep state alive, but the location of that state matters. If the state is declared outside the factory, every returned function can end up sharing it.
Most factories should create a fresh private environment per factory call. That way one configured function cannot pollute another configured function's cache.
Your task: Fix createMultiplier. Each multiplier should cache its own results. Calling one multiplier must not make another multiplier reuse the wrong result for the same input.
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.
Accidental Shared State
Closures are useful because they keep state alive, but the location of that state matters. If the state is declared outside the factory, every returned function can end up sharing it.
Most factories should create a fresh private environment per factory call. That way one configured function cannot pollute another configured function's cache.
Your task: Fix createMultiplier. Each multiplier should cache its own results. Calling one multiplier must not make another multiplier reuse the wrong result for the same input.