forked from JoshCrozier/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2665-counter-ii.js
More file actions
26 lines (25 loc) · 743 Bytes
/
2665-counter-ii.js
File metadata and controls
26 lines (25 loc) · 743 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
/**
* 2665. Counter II
* https://leetcode.com/problems/counter-ii/
* Difficulty: Easy
*
* 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.
*/
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
var createCounter = function(init) {
let current = init;
return {
increment: () => ++current,
decrement: () => --current,
reset: () => current = init,
};
};