Skip to content

Commit e5c74f9

Browse files
committed
solves Challenge-2 (stretch) in closure.js
1 parent 5193062 commit e5c74f9

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎assignments/closure.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ villainDeclarer();
1212

1313
// ==== Challenge 2: Create a counter function ====
1414
const counter = () => {
15-
// Return a function that when invoked increments and returns a counter variable.
15+
let count = 0;
16+
return () => {
17+
return count++;
18+
}
1619
};
17-
// Example usage: const newCounter = counter();
18-
// newCounter(); // 1
19-
// newCounter(); // 2
20+
21+
const newCounter = counter();
22+
23+
console.log(newCounter());
24+
console.log(newCounter());
25+
26+
2027

2128
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
2229
const counterFactory = () => {

0 commit comments

Comments
 (0)