FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Limit

20mins

Implement a function limit(fn, n) that restricts the execution of a given function to at most n times.

The function should:

  • Return a new function that wraps the original function fn.
  • Allow calls to fn only up to n times.
  • After n calls, subsequent invocations should return undefined without executing fn.

Examples

function greet(name) { return 'Hello ' + name; } const limitedGreet = limit(greet, 2); limitedGreet('Alice'); // 'Hello Alice' limitedGreet('Bob'); // 'Hello Bob' limitedGreet('Carol'); // undefined

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests