Shared Closure State
Shared Closure State
Multiple functions returned from the same outer function close over the same variable environment. They do not each get a private copy unless a new outer function call creates one.
This lets a small object expose related methods while keeping the underlying state hidden. The methods coordinate through the shared closed-over variables.
Your task: Complete createDraftCounter. It should return an object with addDraft(), publishOne(), and getCount(). Adding increases the private draft count, publishing decreases it only when the count is above zero, and getCount returns the current count.
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.
Shared Closure State
Multiple functions returned from the same outer function close over the same variable environment. They do not each get a private copy unless a new outer function call creates one.
This lets a small object expose related methods while keeping the underlying state hidden. The methods coordinate through the shared closed-over variables.
Your task: Complete createDraftCounter. It should return an object with addDraft(), publishOne(), and getCount(). Adding increases the private draft count, publishing decreases it only when the count is above zero, and getCount returns the current count.