Java Script Solution for Counter Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the solution is to create a closure that returns a function that i
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the solution is to create a closure that returns a function that increments a counter variable n each time it is called.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach is to define a function createCounter that takes an initial value n as input and returns a function that increments n each time it is called. The returned function is a closure that has access to the n variable defined in the outer function.\nWhen the returned function is called, it increments n by 1 and returns the new value of n. This allows us to create a counter that starts at a given value and increments by 1 each time it is called.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of the createCounter function is O(1), as it simply returns a function without performing any complex operations.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of the createCounter function is O(1), as it only defines a single variable n and returns a function that has access to it.\n\n# Code\n```\n/**\n * @param {number} n\n * @return {Function} counter\n */\nvar createCounter = function(n) {\n    return function() {\n        return n++;\n    };\n};\n\n/** \n * const counter = createCounter(10)\n * counter() // 10\n * counter() // 11\n * counter() // 12\n */\n```
['JavaScript']