Skip to content

Commit 5b39f23

Browse files
authored
code added
1 parent d52e3a0 commit 5b39f23

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

02_counter.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Question Link: https://leetcode.com/problems/counter/description/?envType=study-plan-v2&envId=30-days-of-javascript
2+
// Solution Link: https://leetcode.com/problems/counter/solutions/5424085/easy-javascript-solution/
3+
4+
/*
5+
2620. Counter
6+
7+
Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc).
8+
9+
Example 1:
10+
Input:
11+
n = 10
12+
["call","call","call"]
13+
Output: [10,11,12]
14+
Explanation:
15+
counter() = 10 // The first time counter() is called, it returns n.
16+
counter() = 11 // Returns 1 more than the previous time.
17+
counter() = 12 // Returns 1 more than the previous time.
18+
Example 2:
19+
Input:
20+
n = -2
21+
["call","call","call","call","call"]
22+
Output: [-2,-1,0,1,2]
23+
Explanation: counter() initially returns -2. Then increases after each sebsequent call.
24+
25+
Constraints:
26+
-1000 <= n <= 1000
27+
0 <= calls.length <= 1000
28+
calls[i] === "call"
29+
*/
30+
31+
32+
/**
33+
* @param {number} n
34+
* @return {Function} counter
35+
*/
36+
var createCounter = function(n) {
37+
38+
return function() {
39+
40+
return n++;
41+
};
42+
};
43+
44+
/**
45+
* const counter = createCounter(10)
46+
* counter() // 10
47+
* counter() // 11
48+
* counter() // 12
49+
*/

0 commit comments

Comments
 (0)