Skip to content

Commit 6e08fb7

Browse files
committed
counter
1 parent e0a8464 commit 6e08fb7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Programs/Exercise/counter.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
3+
Given an integer n, return a counter function.
4+
This counter function initially returns n and then returns 1
5+
more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc).
6+
7+
8+
9+
Example 1:
10+
11+
Input:
12+
n = 10
13+
["call","call","call"]
14+
Output: [10,11,12]
15+
*/
16+
17+
var createCounter = function(n) {
18+
19+
return function() {
20+
return n++
21+
};
22+
};
23+
24+
const counter = createCounter(10);
25+
26+
console.log(counter()); // 10
27+
console.log(counter()); // 11
28+
console.log(counter()); // 12
29+
console.log(counter()); // 13

0 commit comments

Comments
 (0)