Skip to content

2665. Counter II 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
type ReturnObj = {
  increment: () => number;
  decrement: () => number;
  reset: () => number;
};

function createCounter(init: number): ReturnObj {
  let cur = init;
  return {
    increment: () => ++cur,
    decrement: () => --cur,
    reset: () => (cur = init),
  };
}