-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounter II
More file actions
31 lines (27 loc) · 696 Bytes
/
Counter II
File metadata and controls
31 lines (27 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
/* 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
*/
const createCounter = (init) => {
let presentCounter = init;
return {
increment: () => ++presentCounter,
decrement: () => --presentCounter,
reset: () => presentCounter = init,
};
};
/**
* const counter = createCounter(5)
* counter.increment(); // 6
* counter.reset(); // 5
* counter.decrement(); // 4
*/