AniUI Academy
easy+150 XPPractice

Count how often a function was called

You suspect a render callback is firing far more often than it should. Rather than littering the function with counters, wrap it: the wrapper does the counting and exposes the tally, and the wrapped function is unchanged.

Implement withCallCount(fn). It returns a wrapper that forwards all of its arguments to fn and returns fn's return value. The wrapper also carries two extra properties: callCount, a number that starts at 0 and rises by one on every call, and reset(), which sets callCount back to 0. Count the call before invoking fn, so a call where fn throws is still counted. Each wrapper keeps its own tally, even when two wrappers wrap the same function.

What it has to do

  • callCount is 0 before the wrapper is called and increases by one per call.
  • The wrapper forwards its arguments and returns fn's return value.
  • A call that throws is still counted, because counting happens before the call.
  • reset() sets callCount back to 0.
  • Two wrappers around the same function keep separate counts.

Your workspace

Try it yourself
Loading playground...

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.