-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2665-CounterII.js
More file actions
96 lines (85 loc) · 2.67 KB
/
2665-CounterII.js
File metadata and controls
96 lines (85 loc) · 2.67 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 2665. 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
// 0 <= calls.length <= 1000
// calls[i] is one of "increment", "decrement" and "reset"
/**
* @param {integer} init
* @return { increment: Function, decrement: Function, reset: Function }
*/
var createCounter = function(init) {
let value = init;
let increment = function() {
return value += 1;
};
let reset = function() {
return value = init;
};
let decrement = function() {
return value -= 1;
};
return {
increment,
reset,
decrement,
}
};
// best solution
var createCounter1 = function(init) {
let origin = init
return{
increment : (value)=>++origin ,
decrement : (value)=>--origin ,
reset : (value)=>origin = init ,
}
};
/**
* const counter = createCounter(5)
* counter.increment(); // 6
* counter.reset(); // 5
* counter.decrement(); // 4
*/
const counter = createCounter(5)
console.log(counter.increment()); // 6
console.log(counter.reset()); // 5
console.log(counter.decrement()); // 4
const counter1 = createCounter(0);
console.log(counter1.increment()); // 1
console.log(counter1.increment()); // 2
console.log(counter1.decrement()); // 1
console.log(counter1.reset()); // 0
console.log(counter1.reset()); // 0
const counter11 = createCounter1(5)
console.log(counter11.increment()); // 6
console.log(counter11.reset()); // 5
console.log(counter11.decrement()); // 4
const counter12 = createCounter1(0);
console.log(counter12.increment()); // 1
console.log(counter12.increment()); // 2
console.log(counter12.decrement()); // 1
console.log(counter12.reset()); // 0
console.log(counter12.reset()); // 0