{ "data": { "question": { "questionId": "2789", "questionFrontendId": "2665", "categoryTitle": "JavaScript", "boundTopicId": 2258605, "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.

\n\n

The three functions are:

\n\n\n\n

 

\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\n", "translatedTitle": "计数器 II", "translatedContent": "

请你写一个函数 createCounter。这个函数接收一个初始的整数值 init。并返回一个包含三个函数的对象。

\n\n

这三个函数是:

\n\n\n\n

 

\n\n

示例 1:

\n\n
\n输入:init = 5, calls = [\"increment\",\"reset\",\"decrement\"]\n输出:[6,5,4]\n解释:\nconst counter = createCounter(5);\ncounter.increment(); // 6\ncounter.reset(); // 5\ncounter.decrement(); // 4\n
\n\n

示例 2:

\n\n
\n输入:init = 0, calls = [\"increment\",\"increment\",\"decrement\",\"reset\",\"reset\"]\n输出:[1,2,1,0,0]\n解释:\nconst counter = createCounter(0);\ncounter.increment(); // 1\ncounter.increment(); // 2\ncounter.decrement(); // 1\ncounter.reset(); // 0\ncounter.reset(); // 0\n
\n\n

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 4, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {integer} init\n * @return { increment: Function, decrement: Function, reset: Function }\n */\nvar createCounter = function(init) {\n \n};\n\n/**\n * const counter = createCounter(5)\n * counter.increment(); // 6\n * counter.reset(); // 5\n * counter.decrement(); // 4\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type ReturnObj = {\n increment: () => number,\n decrement: () => number,\n reset: () => number,\n}\n\nfunction createCounter(init: number): ReturnObj {\n\t\n};\n\n/**\n * const counter = createCounter(5)\n * counter.increment(); // 6\n * counter.reset(); // 5\n * counter.decrement(); // 4\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"8.4K\", \"totalSubmission\": \"13.2K\", \"totalAcceptedRaw\": 8425, \"totalSubmissionRaw\": 13192, \"acRate\": \"63.9%\"}", "hints": [ "You can return an object with methods.", "Initialize a variable for currentCount. Inside these methods, add the appropriate logic which mutates currentCount." ], "solution": null, "status": null, "sampleTestCase": "5\n[\"increment\",\"reset\",\"decrement\"]", "metaData": "{\n \"name\": \"createCounter\",\n \"params\": [\n {\n \"name\": \"init\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"string[]\",\n \"name\": \"calls\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"

TypeScript 5.1.6<\\/p>\\r\\n\\r\\n

Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n

lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "5\n[\"increment\",\"reset\",\"decrement\"]\n0\n[\"increment\",\"increment\",\"decrement\",\"reset\",\"reset\"]", "__typename": "QuestionNode" } } }