{ "data": { "question": { "questionId": "2789", "questionFrontendId": "2665", "boundTopicId": null, "title": "Counter II", "titleSlug": "counter-ii", "content": "
Write a function createCounter
. It should accept an initial integer init
. It should return an object with three functions.
The three functions are:
\n\nincrement()
increases the current value by 1 and then returns it.decrement()
reduces the current value by 1 and then returns it.reset()
sets the current value to init
and then returns it.\n
Example 1:
\n\n\nInput: init = 5, calls = ["increment","reset","decrement"]\nOutput: [6,5,4]\nExplanation:\nconst counter = createCounter(5);\ncounter.increment(); // 6\ncounter.reset(); // 5\ncounter.decrement(); // 4\n\n\n
Example 2:
\n\n\nInput: init = 0, calls = ["increment","increment","decrement","reset","reset"]\nOutput: [1,2,1,0,0]\nExplanation:\nconst counter = createCounter(0);\ncounter.increment(); // 1\ncounter.increment(); // 2\ncounter.decrement(); // 1\ncounter.reset(); // 0\ncounter.reset(); // 0\n\n\n
\n
Constraints:
\n\n-1000 <= init <= 1000
0 <= calls.length <= 1000
calls[i]
is one of "increment", "decrement" and "reset"Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 5.1.6, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2022 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }