FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Easy

Simple Counter

5 min

Design a function createCounter that takes an optional starting number. The function should return another function which, when called for the first time, returns the starting number (or 0 if no starting number is given). Each subsequent call should return the next integer in sequence, increasing by 1 from the previous value.

Example:

const countUp = createCounter(); countUp(); // 0 countUp(); // 1 countUp(); // 2 const countFromFive = createCounter(5); countFromFive(); // 5 countFromFive(); // 6 countFromFive(); // 7

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests