Skip to content

Commit 0099f95

Browse files
committed
stretch ft(create a counter function)
1 parent 4a01b00 commit 0099f95

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎assignments/closure.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ console.log(school(2)());
1515
// ==== Challenge 2: Create a counter function ====
1616
const counter = () => {
1717
// Return a function that when invoked increments and returns a counter variable.
18+
let counter = 0
19+
const counterFunction = () => {
20+
counter = counter + 1
21+
return counter
22+
}
23+
return counterFunction
1824
};
25+
const newCounter = counter();
26+
console.log(newCounter());
27+
console.log(newCounter());
28+
console.log(newCounter());
1929
// Example usage: const newCounter = counter();
2030
// newCounter(); // 1
2131
// newCounter(); // 2

0 commit comments

Comments
 (0)