-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge-33.js
42 lines (34 loc) · 995 Bytes
/
Challenge-33.js
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
// 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).
// Example 1:
// Input:
// n = 10
// ["call","call","call"]
// Output: [10,11,12]
// Explanation:
// counter() = 10 // The first time counter() is called, it returns n.
// counter() = 11 // Returns 1 more than the previous time.
// counter() = 12 // Returns 1 more than the previous time.
// Example 2:
// Input:
// n = -2
// ["call","call","call","call","call"]
// Output: [-2,-1,0,1,2]
// Explanation: counter() initially returns -2. Then increases after each sebsequent call.
// Constraints:
// -1000 <= n <= 1000
// 0 <= calls.length <= 1000
// calls[i] === "call"
// #solution
const createCounter = function (n) {
let i = 0;
let count = n;
return function () {
n = count + i;
i++;
return n;
};
};
let counter = createCounter(10);
counter();
counter();
counter();