easy 0 views
Counter II
Write a function createCounter.
Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.
The three functions are:
Example 1
Input: init = 5, calls = ["increment","reset","decrement"]
Output: [6,5,4]
Explanation: const counter = createCounter(5); counter.increment(); // 6 counter.reset(); // 5 counter.decrement(); // 4
Example 2
Input: init = 0, calls = ["increment","increment","decrement","reset","reset"]
Output: [1,2,1,0,0]
Explanation: const counter = createCounter(0); counter.increment(); // 1 counter.increment(); // 2 counter.decrement(); // 1 counter.reset(); // 0 counter.reset(); // 0
Constraints
- -1000 <= init <= 1000
- 0 <= calls.length <= 1000
- calls[i] is one of "increment", "decrement" and "reset"
Hints
Companies
No companies reported yet.
Discussion
Sign in to join the discussion.
Loading discussion...
Test results
No test cases yet.