Prototype: retainRecent(values, capacity) returns a new array containing the newest at most capacity items in original order. capacity is a nonnegative safe integer or throws RangeError. capacity=0 returns []; preserve item identity and caller input.

Final: implement EventHub with subscribe(listener), publish(value), size(), and dispose(). Each registration is independent, even for the same function. subscribe returns an idempotent unsubscribe function. publish snapshots current registrations, invokes all of them in registration order (including one removed during this publish), and reports listener failures together in AggregateError after invoking the rest. A listener added during publish starts with the next publish. dispose is idempotent, removes all registrations, and makes future subscribe throw Error. If a listener calls dispose during publish, all remaining listeners in that current snapshot still run (and their failures still aggregate); only later publish calls do nothing.

Complete the exported stubs in exercise.mjs. The prototype and final stages have separate checks.

Shell
node main.mjs --stage prototype
node main.mjs --stage final

Use Node.js 20+ locally, or the website's browser worker. Tests are standard-library only and check the documented behavior; they do not prove performance or force garbage collection.

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
No results yet.

Complete the starter code, then run the checks.
Results and errors will appear here.