Day 8: Limit api calls

1 min read ❤️ 2 · 💬 1
#javascript #day8 #100daysofcode

Question

Create a function called limitCalls that takes a function and a maximum number of calls as arguments. The function should return a closure that only invokes the original function up to the specified maximum number of times.

function logMessage(message) {
  console.log(message);
}

const limitedLogMessage = limitCalls(logMessage, 3);

limitedLogMessage('Hello'); // Output: Hello
limitedLogMessage('World'); // Output: World
limitedLogMessage('OpenAI'); // Output: Challenge
limitedLogMessage('Extra call'); // Output: Maximum number of calls reached!
Enter fullscreen mode Exit fullscreen mode

🤫 Check the comment below to see answer.


Originally published on DEV Community . Read the discussion and share your thoughts there.

© 2026

LinkedIn 𝕏 GitHub