Sobes.tech
Junior — Senior

Counter factory with operations and deferred output

livecode

Task condition

Implement a function createCounter that generates a counter object with three methods:

  • increment() — adds 1 to the current value;
  • decrement() — subtracts 1 from the current value;
  • log() — outputs the current value to the console after a delay of one second.
const counter = createCounter(10);

counter.increment();
counter.increment();
counter.decrement();

counter.log(); // After 1 second, the console will display: 10 + 1 + 1 - 1 = 11

The function should work correctly with any initial number, supporting multiple independent counters.