Write a function `createCounter(start)` that returns an object with `increment()`, `decrement()`, and `getCount()` methods using closure.
Problem Statement
Write a function `createCounter(start)` that returns an object with `increment()`, `decrement()`, and `getCount()` methods using closure.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Closures capture variables from the enclosing scope.
š” Hint 2:
The counter variable persists between calls because of closure.
š” Hint 3:
let count = start; return { increment: () => ++count, decrement: () => --count, getCount: () => count };
ā
Solution:
```javascript
function createCounter(start) {
let count = start;
return {
increment: () => ++count,
decrement: () => --count,
getCount: () => count
};
}
```