{ "data": { "question": { "questionId": "2732", "questionFrontendId": "2620", "boundTopicId": null, "title": "Counter", "titleSlug": "counter", "content": "

Given an integer n, return a counter function. This counter function initially returns n and then returns 1 more than the previous value every subsequent time it is called (n, n + 1, n + 2, etc).

\n\n

 

\n

Example 1:

\n\n
\nInput: \nn = 10 \n["call","call","call"]\nOutput: [10,11,12]\nExplanation: \ncounter() = 10 // The first time counter() is called, it returns n.\ncounter() = 11 // Returns 1 more than the previous time.\ncounter() = 12 // Returns 1 more than the previous time.\n
\n\n

Example 2:

\n\n
\nInput: \nn = -2\n["call","call","call","call","call"]\nOutput: [-2,-1,0,1,2]\nExplanation: counter() initially returns -2. Then increases after each sebsequent call.\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 989, "dislikes": 71, "isLiked": null, "similarQuestions": "[{\"title\": \"Memoize\", \"titleSlug\": \"memoize\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Function Composition\", \"titleSlug\": \"function-composition\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Counter II\", \"titleSlug\": \"counter-ii\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "exampleTestcases": "10\n[\"call\",\"call\",\"call\"]\n-2\n[\"call\",\"call\",\"call\",\"call\",\"call\"]", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number} n\n * @return {Function} counter\n */\nvar createCounter = function(n) {\n \n return function() {\n \n };\n};\n\n/** \n * const counter = createCounter(10)\n * counter() // 10\n * counter() // 11\n * counter() // 12\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function createCounter(n: number): () => number {\n \n return function() {\n\t\t\n }\n}\n\n\n/** \n * const counter = createCounter(10)\n * counter() // 10\n * counter() // 11\n * counter() // 12\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"204.4K\", \"totalSubmission\": \"257K\", \"totalAcceptedRaw\": 204373, \"totalSubmissionRaw\": 257025, \"acRate\": \"79.5%\"}", "hints": [ "In JavaScript, a function can return a closure. A closure is defined as a function and the variables declared around it (it's lexical environment).", "A count variable can be initialized in the outer function and mutated in the inner function." ], "solution": { "id": "1878", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "10\n[\"call\",\"call\",\"call\"]", "metaData": "{\n \"name\": \"createCounter\",\n \"params\": [\n {\n \"name\": \"n\",\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, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For 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.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }