-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.js
44 lines (35 loc) · 1.07 KB
/
Counter.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
43
44
/*
LeetCode Task:
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).
*/
// Increment Then Return
// const createCounter = function(n) {
// let currentCount = n - 1;
// return function() {
// currentCount += 1;
// return currentCount;
// };
// };
// Postfix Increment Syntax
// const createCounter = function(n) {
// return function() {
// return n++;
// }
// }
// Prefix Decrement and Increment Syntax
// const createCounter = function(n) {
// --n;
// return function() {
// return ++n;
// }
// }
// Postfix Increment Syntax With Arrow Function
// const createCounter = function(n) {
// return () => n++
// }
const createCounter = n => () => n++;
const counter = createCounter(10)
console.log(counter())
console.log(counter())
console.log(counter())