Sobes.tech
Middle

Implement the callLimit function, which limits the number of calls to the given function to a specified limit, and also provides a reset method to reset the counter.

sobes.tech AI

Answer from AI

javascript function callLimit(fn, limit) { let count = 0;

function limited(...args) { if (count < limit) { count++; return fn(...args); } // You can return undefined or throw an error if the limit is exceeded }

limited.reset = () => { count = 0; };

return limited; }

// Example usage: const sayHello = () => console.log('Hello'); const limitedHello = callLimit(sayHello, 3);

limitedHello(); // Hello limitedHello(); // Hello limitedHello(); // Hello limitedHello(); // will not be called

limitedHello.reset(); limitedHello(); // Hello