Skip to content

Commit 07ad05a

Browse files
committed
complete closure.js
1 parent 8f83f79 commit 07ad05a

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

assignments/closure.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,33 @@ worldBest()
1414

1515

1616
// ==== Challenge 2: Implement a "counter maker" function ====
17-
const counterMaker = () => {
17+
let count = 0;
18+
const counterMaker = (limit) => {
1819
// IMPLEMENTATION OF counterMaker:
1920
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
2021
// 2- Declare a function `counter`. It should increment and return `count`.
2122
// NOTE: This `counter` function, being nested inside `counterMaker`,
2223
// "closes over" the `count` variable. It can "see" it in the parent scope!
2324
// 3- Return the `counter` function.
25+
// let count = 0;
26+
function counter() {
27+
count++;
28+
return count;
29+
}
30+
31+
return counter();
2432
};
2533
// Example usage: const myCounter = counterMaker();
34+
// const myCounter = counterMaker();
35+
// const myCounter = counterMaker;
2636
// myCounter(); // 1
2737
// myCounter(); // 2
38+
console.log(counterMaker())
39+
console.log(counterMaker())
40+
console.log(counterMaker())
41+
42+
43+
2844

2945
// ==== Challenge 3: Make `counterMaker` more sophisticated ====
3046
// It should have a `limit` parameter. Any counters we make with `counterMaker`

0 commit comments

Comments
 (0)