✔💯 DAY 401 | MULTIPLE WAYS | [JAVASCRIPT/TYPESCRIPT] | 100% 0ms | EXPLAINED | Approach 🆙🆙🆙
Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n Describe your approach to solving the problem. \n\n\n\n# Complexity\n- Time compl
# Please Upvote as it really motivates me \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\n<!-- Describe your approach to solving the problem. -->\n![image.png](https://assets.leetcode.com/users/images/1e873636-cdf1-41ef-abbd-caed210b7000_1683342340.5855558.png)\n\n\n# Complexity\n- Time complexity:o(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:o(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code Using a function expression assigned to a variable:\n```js []\n/**\n * @param {number} n\n * @return {Function} counter\n */\nvar createCounter = function(n){\n    let c=n-1;\n    return function(){\n        c++;\n        return c;\n    }\n}\n\n/** \n * const counter = createCounter(10)\n * counter() // 10\n * counter() // 11\n * counter() // 12\n */\n```\n```ts []\nlet createCounter = function(n: number): () => number {\n    let c = n - 1;\n    return function(): number {\n        c++;\n        return c;\n    }\n};\n```\n# 2nd Using a function declaration:\n```js  []\nfunction createCounter(n) {\n    let c = n - 1;\n    return function() {\n        c++;\n        return c;\n    }\n}\n```\n```ts []\nfunction createCounter(n: number): () => number {\n    let c = n - 1;\n    return function(): number {\n        c++;\n        return c;\n    }\n}\n```\n# 3rd Using an arrow function assigned to a variable:\n```Js []\nlet createCounter = n => {\n    let c = n - 1;\n    return () => {\n        c++;\n        return c;\n    }\n};\n```\n```ts []\nlet createCounter = (n: number): (() => number) => {\n    let c = n - 1;\n    return (): number => {\n        c++;\n        return c;\n    }\n};\n```\n\n\n# Intuition & Approach\nAll three versions of the createCounter function take an integer  n as input and return a function that increments and returns a counter value starting from n . The counter value is initialized to  n-1 to ensure that the first call to the counter function returns  n\n\n\n# one liner \n```js []\nvar createCounter = (n) => () => n++;\n```\n```ts []\nlet createCounter = (n: number): (() => number) => () => n++;\n```\n\n![BREUSELEE.webp](https://assets.leetcode.com/users/images/062630f0-ef80-4e74-abdb-302827b99235_1680054012.5054147.webp)\n![image.png](https://assets.leetcode.com/users/images/303fa18d-281d-49f0-87ef-1a018fc9a488_1681355186.0923774.png)\n\n\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D \uD83C\uDD99\uD83C\uDD99\uD83C\uDD99\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n.
['TypeScript', 'JavaScript']