Skip to content

Commit 096c29b

Browse files
committed
closures update with cleaner solution for challenge 3
1 parent fe75a44 commit 096c29b

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

‎assignments/closure.js‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,14 @@ console.log(counter());
2323
// Return an object that has two methods called `increment` and `decrement`.
2424
// `increment` should increment a counter variable in closure scope and return it.
2525
// `decrement` should decrement the counter variable and return it.
26-
let num = 0;
27-
const counterFunction = () => {
28-
function increment(){
29-
num++;
30-
return num;
31-
}
32-
return increment();
26+
const counterFactory = () => {
27+
let count = 0;
28+
return {
29+
increment: () => (++count),
30+
decrement: () => (--count),
31+
};
3332

34-
function decrement(){
35-
num--;
36-
return num;
37-
}
38-
return decrement();
39-
}
40-
41-
console.log(counterFunction('increment')); //?
33+
const newCounterFactory = counterFactory();
34+
newCounterFactory.increment(); // 1
35+
newCounterFactory.decrement(); // 0
36+
newCounterFactory.decrement(); // -1

0 commit comments

Comments
 (0)