2789. Counter II
My accepted JavaScript solution to LeetCode problem 2789, Counter II, running in 40ms.
- Difficulty: Easy
- JavaScript
- Runtime 40ms
- Memory 57.5MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 40ms, memory 57.5MB, accepted 2025-12-31.
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
var createCounter = function(init) {
let current = init;
return {
increment: function() {
return ++current;
},
decrement: function() {
return --current;
},
reset: function() {
current = init;
return current;
}
};
};
/**
* const counter = createCounter(5)
* counter.increment(); // 6
* counter.reset(); // 5
* counter.decrement(); // 4
*/