# 2665. Counter II
###### tags: `leetcode 30 days js challenge` `Easy`
[2665. Counter II](https://leetcode.com/problems/counter-ii/)
### 題目描述
Write a function `createCounter`. It should accept an initial integer `init`. It should return an object with three functions.
The three functions are:
* `increment()` increases the current value by 1 and then returns it.
* `decrement()` reduces the current value by 1 and then returns it.
* `reset()` sets the current value to `init` and then returns it.
### 範例
**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`
- `total calls not to exceed 1000`
### 解答
#### TypeScript
```typescript=
type ReturnObj = {
increment: () => number;
decrement: () => number;
reset: () => number;
};
function createCounter(init: number): ReturnObj {
let count = init;
const increment = () => ++count;
const decrement = () => --count;
const reset = () => (count = init);
return {
increment,
decrement,
reset,
};
}
```
> [name=Sheep][time=Sun, May 7, 2023]
### Reference
[回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)