Skip to content

Commit 4810cf9

Browse files
authored
Create Counter
1 parent c32a7f8 commit 4810cf9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Counter

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number} n
3+
* @return {Function} counter
4+
*/
5+
6+
/* Example 1:
7+
8+
Input:
9+
n = 10
10+
["call","call","call"]
11+
Output: [10,11,12]
12+
Explanation:
13+
counter() = 10 // The first time counter() is called, it returns n.
14+
counter() = 11 // Returns 1 more than the previous time.
15+
counter() = 12 // Returns 1 more than the previous time.
16+
17+
*/
18+
19+
/* var createCounter = function(n) {
20+
return function() {
21+
return n++;
22+
};
23+
};
24+
*/
25+
const createCounter = (n) => () => n++;
26+
27+
/**
28+
* const counter = createCounter(10)
29+
* counter() // 10
30+
* counter() // 11
31+
* counter() // 12
32+
*/

0 commit comments

Comments
 (0)