Allow only N calls
Sometimes the rule is not "once" but "three times": three hint reveals, five undo steps, two automatic retries. The counter belongs in the wrapper, not scattered through the caller.
Implement limitCalls(fn, max). It returns a wrapper that invokes fn for the first max calls, forwarding all arguments and preserving this, and returns fn's return value. From call max + 1 onwards fn is not invoked at all and the wrapper returns the value returned by the last successful invocation. When max is 0, fn is never invoked and the wrapper returns undefined. The wrapper also carries a remaining property: the number of invocations still allowed, which starts at max, drops by one per allowed call and never goes below 0.
What it has to do
- Invoke
fnfor the firstmaxcalls only. - After the limit, return the last successful result without invoking
fn. - With
maxof0, never invokefnand returnundefined. - Keep a
remainingproperty up to date, never dropping below0. - Forward arguments and preserve
this.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.