Easy | Basic - Calculator with Method Chaining by JS
ApproachThis Calculator class implements a chainable API for basic arithmetic operations—addition, subtraction, multiplication, division, and exponentiation—on
# Approach\nThis Calculator class implements a chainable API for basic arithmetic operations\u2014addition, subtraction, multiplication, division, and exponentiation\u2014on a result value, with built-in error handling for division by zero.\n\n# Complexity\n- Time complexity: ***O(1)***\n\n- Space complexity: ***O(1)***\n\n# Screenshot:\n![image.png](https://assets.leetcode.com/users/images/3e9f21f0-a50f-428b-b14d-116fbd9e24aa_1734240406.7221346.png)\n\n\n# Code\n```javascript []\nclass Calculator {\n    // @param {number} value\n    constructor(value) {\n        this.result = value;\n    }\n    \n    //-------- @return {Calculator} -------- STARTS\n    // @param {number} value\n    add(value){\n        this.result += value;\n        return this;\n    }\n    \n    // @param {number} value\n    subtract(value){\n        this.result -= value;\n        return this;\n    }\n    \n    // @param {number} value \n    multiply(value) {\n        this.result *= value;\n        return this;\n    }\n    \n    // @param {number} value\n    divide(value) {\n        if(value == 0) throw new Error(\'Division by zero is not allowed\');\n        this.result /= value;\n        return this;\n    }\n    \n    // @param {number} value\n    power(value) {\n        this.result = Math.pow(this.result, value);\n        return this;\n    }\n    \n    //-------- @return {Calculator} -------- ENDS\n\n    // @return {number}\n    getResult() {\n        return this.result;\n    }\n}\n```\n
['JavaScript']