Skip to content

Commit 58d1d1e

Browse files
committed
Make notes for the closures stretch problems.
1 parent 8909908 commit 58d1d1e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

assignments/closure.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
66

77

8-
// ==== Challenge 2: Create a counter function ====
9-
const counter = () => {
10-
// Return a function that when invoked increments and returns a counter variable.
8+
// ==== Challenge 2: Implement a "counter maker" function ====
9+
const counterMaker = () => {
10+
// IMPLEMENTATION OF counterMaker:
11+
// 1- Declare a `count` variable with an value of 0. We will be mutating it, so declare it using `let`!
12+
// 2- Declare a function `counter`. It should increment and return `count`.
13+
// NOTE: This `counter` function, being nested inside `counterMaker`,
14+
// "closes over" the`count` variable. It can "see" it in the parent scope!
15+
// 3- Return the `counter` function.
1116
};
12-
// Example usage: const newCounter = counter();
13-
// newCounter(); // 1
14-
// newCounter(); // 2
17+
// Example usage: const myCounter = counterMaker();
18+
// myCounter(); // 1
19+
// myCounter(); // 2
1520

16-
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
21+
// ==== Challenge 3: Make `counterMaker` more sophisticated ====
22+
// It should take a `limit` argument. Any counters we make with `counterMaker`
23+
// will refuse to go over the limit, and start back at 1.
24+
25+
// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
1726
const counterFactory = () => {
1827
// Return an object that has two methods called `increment` and `decrement`.
1928
// `increment` should increment a counter variable in closure scope and return it.

0 commit comments

Comments
 (0)