Skip to content

Commit 0e12e3d

Browse files
authored
closure homework complete
1 parent 71cc59f commit 0e12e3d

1 file changed

Lines changed: 62 additions & 38 deletions

File tree

‎src/closure.js‎

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,62 @@
1-
// Complete the following functions.
2-
3-
const counter = () => {
4-
// Return a function that when invoked increments and returns a counter variable.
5-
// Example: const newCounter = counter();
6-
// newCounter(); // 1
7-
// newCounter(); // 2
8-
};
9-
10-
const counterFactory = () => {
11-
// Return an object that has two methods called `increment` and `decrement`.
12-
// `increment` should increment a counter variable in closure scope and return it.
13-
// `decrement` should decrement the counter variable and return it.
14-
};
15-
16-
const limitFunctionCallCount = (cb, n) => {
17-
// Should return a function that invokes `cb`.
18-
// The returned function should only allow `cb` to be invoked `n` times.
19-
};
20-
21-
/* Extra Credit */
22-
const cacheFunction = (cb) => {
23-
// Should return a funciton that invokes `cb`.
24-
// A cache (object) should be kept in closure scope.
25-
// The cache should keep track of all arguments have been used to invoke this function.
26-
// If the returned function is invoked with arguments that it has already seen
27-
// then it should return the cached result and not invoke `cb` again.
28-
// `cb` should only ever be invoked once for a given set of arguments.
29-
};
30-
31-
/* eslint-enable no-unused-vars */
32-
33-
module.exports = {
34-
counter,
35-
counterFactory,
36-
cacheFunction,
37-
limitFunctionCallCount
38-
};
1+
// Complete the following functions.
2+
3+
const counter = () => {
4+
// Return a function that when invoked increments and returns a counter variable.
5+
// Example: const newCounter = counter();
6+
// newCounter(); // 1
7+
// newCounter(); // 2
8+
let count = 0;
9+
return () => count += 1;
10+
};
11+
12+
const counterFactory = () => {
13+
// Return an object that has two methods called `increment` and `decrement`.
14+
// `increment` should increment a counter variable in closure scope and return it.
15+
// `decrement` should decrement the counter variable and return it.
16+
let count = 0;
17+
return {
18+
increment: () => count += 1,
19+
decrement: () => count -= 1
20+
};
21+
};
22+
23+
const limitFunctionCallCount = (cb, n) => {
24+
// Should return a function that invokes `cb`.
25+
// The returned function should only allow `cb` to be invoked `n` times.
26+
let count = 0;
27+
return (...args) => {
28+
if (count === n) {
29+
return null;
30+
}
31+
count += 1;
32+
return cb(...args);
33+
};
34+
};
35+
36+
/* Extra Credit */
37+
const cacheFunction = (cb) => {
38+
// Should return a funciton that invokes `cb`.
39+
// A cache (object) should be kept in closure scope.
40+
// The cache should keep track of all arguments have been used to invoke this function.
41+
// If the returned function is invoked with arguments that it has already seen
42+
// then it should return the cached result and not invoke `cb` again.
43+
// `cb` should only ever be invoked once for a given set of arguments.
44+
const storage = {};
45+
46+
return (input) => {
47+
if (Object.prototype.hasOwnProperty.call(storage, input)) {
48+
return storage[input];
49+
}
50+
storage[input] = cb(input);
51+
return storage[input];
52+
};
53+
};
54+
55+
/* eslint-enable no-unused-vars */
56+
57+
module.exports = {
58+
counter,
59+
counterFactory,
60+
cacheFunction,
61+
limitFunctionCallCount
62+
};

0 commit comments

Comments
 (0)