Sobes.tech
Intern — Senior

Counter implemented via closure

livecode

Task condition

In this task, you need to implement a simple counter using closure capabilities in JavaScript. The function should return an object with the current value of the counter and methods to increase and decrease this value.

const makeCounter = () => {
    let current = 0;

    const inc = () => {
        current++;
    };

    const dec = () => {
        current--;
    };

    return {
        current,
        inc,
        dec
    };
};

const counter = makeCounter();

counter.inc();
counter.inc();
counter.dec();

// console.log(counter.current);