2726. Calculator with Method Chaining
IntuitionApproachExplanation:Error Handling in Constructor:
An error property is added to the constructor to store error messages.
divide Method:

If the diviso
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n# Explanation:\n**Error Handling in Constructor:**\n- An error property is added to the constructor to store error messages.\n\n**divide Method:**\n\n- If the divisor is zero, the error message "Division by zero is not allowed" is stored in the error property.\n\n- If the divisor is not zero, the value is divided normally.\n\n**getResult Method:**\n\n- Checks if there is an error message. If so, it returns the error message.\n\n- If no error, it returns the current value.\n\n# Example Usage:\n- In the first example, attempting to divide by zero sets the error message and getResult returns "Division by zero is not allowed".\n\n- In the second example, performing a series of valid operations and then calling getResult returns the final result of the calculations.\n<!-- Describe your approach to solving the problem. -->\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\n```javascript []\nclass Calculator {\n    /** \n     * @param {number} value\n     */\n    constructor(value = 0) {\n        this.value = value;\n        this.error = null;  // Add an error property to store error messages\n    }\n    \n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    add(value) {\n        this.value += value;\n        return this;\n    }\n    \n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    subtract(value) {\n        this.value -= value;\n        return this;\n    }\n    \n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */  \n    multiply(value) {\n        this.value *= value;\n        return this;\n    }\n    \n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    divide(value) {\n        if (value !== 0) {\n            this.value /= value;\n        } else {\n            this.error = "Division by zero is not allowed"; // Store the error message\n        }\n        return this;\n    }\n    \n    /** \n     * @param {number} value\n     * @return {Calculator}\n     */\n    power(value) {\n        this.value = Math.pow(this.value, value);\n        return this;\n    }\n    \n    /** \n     * @return {number|string}\n     */\n    getResult() {\n        if (this.error) {\n            return this.error; // Return the error message if there\'s one\n        }\n        return this.value;\n    }\n}\n\n// Example usage:\n// const result1 = new Calculator(20)\n//     .divide(0)\n//     .getResult();\n\n// console.log(result1); // Output: "Division by zero is not allowed"\n\n// const result2 = new Calculator(2)\n//     .add(3)\n//     .subtract(1)\n//     .multiply(10)\n//     .divide(2)\n//     .power(2)\n//     .getResult();\n\n// console.log(result2); // Output: 400\n\n```
['JavaScript']