 Code 2726: Calculator with Method Chaining | easy | with explanation
IntuitionBoiler plate is already created just need to perform operations accordingly.Approachyou just need to add basic operation.create an error so that in cas
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBoiler plate is already created just need to perform operations accordingly.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nyou just need to add basic operation.create an error so that in case if any error encounters just add that error into that else add the value in value.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of calling a method in a class depends on the operations performed inside the method. However, the act of calling the method itself is generally considered to have a constant time complexity, O(1)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe act of calling a method itself doesn\'t require significant memory, so the base space complexity is constant, \nO(1)\n# Code\n```javascript []\nclass Calculator {\n    \n    /** \n     * @param {number} value\n     */\n    constructor(value) {\n        this.value = value;\n        this.error = null;\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.error = "Division by zero is not allowed"\n        }else{\n            this.value /= value;\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}\n     */\n    getResult() {\n        if(this.error){\n            return this.error;\n        }\n        return this.value;\n    }\n}\n```
['JavaScript']