FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Argument Stats

20mins

Implement a function argumentStats(fn) that wraps a function fn and returns a new function.

The wrapped function should:

  • Track every call to the function.
  • Flatten any nested arrays in the arguments.
  • Return the result of the original function.

Additionally, the wrapped function should provide two methods:

  • .totalCalls(): returns the total number of times the function has been called.
  • .totalArgs(): returns the total number of arguments (after flattening) across all calls.

Example

function sum(a, b) { return a + b; } const wrappedSum = argumentStats(sum); wrappedSum(1, 2); // 3 wrappedSum([3, 4], 5); // 12 wrappedSum.totalCalls(); // 2 wrappedSum.totalArgs(); // 5 (1,2,3,4,5)

Hints

  • Use rest parameters (...args) to capture arguments.
  • Flatten arrays recursively to count all arguments.
  • Store call count and total argument count in closure.

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests