Set and decrease for counter
importance: 5
Modify the code of makeCounter()
so that the counter can also decrease and set the number:
counter()
should return the next number (as before).counter.set(value)
should set the counter tovalue
.counter.decrease()
should decrease the counter by 1.
See the sandbox code for the complete usage example.
P.S. You can use either a closure or the function property to keep the current count. Or write both variants.
The solution uses count
in the local variable, but addition methods are written right into the counter
. They share the same outer lexical environment and also can access the current count
.
function makeCounter() {
let count = 0;
function counter() {
return count++;
}
counter.set = value => count = value;
counter.decrease = () => count--;
return counter;
}