Detailed JavaScript Solution | Calculator with Method Chaining
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to design a Calculator class that supports basic arithmetic operations and meth
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to design a `Calculator` class that supports basic arithmetic operations and method chaining.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. This initializes a new instance of the `Calculator` class with the provided `value` as the starting `result`\n\n    ```\n    /** \n     * @param {number} value\n     */\n    constructor(value) {\n        this.result = value\n    }\n    ```\n\n2. `add` \u2192 this method adds the given number `value` to the `result` and returns the updated `Calculator`\n\n    ```\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    add(value){\n        this.result += value;\n        return this\n    }\n    ```\n\n3. `subtract` \u2192 this method subtracts the given number `value` from the `result` and returns the updated `Calculator`\n\n    ```\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    subtract(value){\n        this.result -= value\n        return this\n    }\n    ```\n\n4. `multiply` \u2192 this method multiplies the `result`  by the given number `value` and returns the updated `Calculator`\n\n    ```\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */  \n    multiply(value) {\n        this.result *= value\n        return this\n    }\n    ```\n\n5. `divide` \u2192 this method divides the `result` by the given number `value` and returns the updated `Calculator`\n\n    ```\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    divide(value) {\n        if(value === 0 ) throw new Error("Division by zero is not allowed") \n        this.result  = this.result / value\n        return this\n    }\n    ```\n\n6. `power` \u2192 this method raises the `result` to the power of the given number `value` and returns the updated `Calculator`\n\n    ```\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    power(value) {\n        this.result = Math.pow(this.result,value)\n        return this\n    }\n    ```\n\n7. `getResult` \u2192 this method returns the `result`\n\n    ```\n    /** \n     * @return {number}\n     */\n    getResult() {\n        return this.result\n    }\n    ```\n\n\n# Complexity\n- Time complexity: $O(1)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThis is because the methods perform a single mathematical operation and update the variable, which takes a constant amount of time regardless of the size of the input.\n\n- Space complexity: $O(1)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThis is because the class only uses a single variable to store the current value.\n\n# Code\n```\nclass Calculator {\n\n    /** \n     * @param {number} value\n     */\n    constructor(value) {\n        this.result = value\n    }\n\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    add(value){\n        this.result += value;\n        return this\n    }\n\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    subtract(value){\n        this.result -= value\n        return this\n    }\n\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */  \n    multiply(value) {\n        this.result *= value\n        return this\n    }\n\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    divide(value) {\n        if(value === 0 ) throw new Error("Division by zero is not allowed") \n        this.result  = this.result / value\n        return this\n  }\n\n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    power(value) {\n        this.result = Math.pow(this.result,value)\n        return this\n    }\n\n    /** \n     * @return {number}\n     */\n    getResult() {\n        return this.result\n    }\n}\n```
['JavaScript']