const counter = (function () {
let count = 0;
const history = [];
return {
increment(step = 1) {
count += step;
history.push(count);
return this;
},
decrement(step = 1) {
count -= step;
history.push(count);
return this;
},
getHistory: () => history,
getCount: () => count,
};
})();
counter.increment(5).increment(3).decrement(2);
console.log(counter.getCount(), counter.getHistory().join(" -> ")); Post #3162
2.38K
CHALLENGE
- ❤ 5
- 👍 1