Chained Calculator: Fluent Arithmetic with a Twist
Intuition\nThe problem requires implementing a calculator with method chaining, allowing for multiple operations to be performed sequentially. The idea is to us
# Intuition\nThe problem requires implementing a calculator with method chaining, allowing for multiple operations to be performed sequentially. The idea is to use class methods that return the object itself (`this`) to enable chaining. \n\n# Approach\n1. **Encapsulation of Value**: Store the initial value in a private property, which will be updated with each operation.\n2. **Method Chaining**: Each operation (`add`, `subtract`, `multiply`, `divide`, `power`) modifies the current value and returns the instance (`this`) to allow further operations.\n3. **Division Check**: Handle edge cases like division by zero by throwing an error.\n4. **Final Result**: Provide a `getResult` method to retrieve the computed value after a chain of operations.\n\nThis approach ensures modularity and a clean interface for performing calculations.\n\n# Complexity\n- Time complexity:\n  - Each operation (`add`, `subtract`, `multiply`, `divide`, `power`): $$O(1)$$, as these are constant-time operations.\n  - `getResult`: $$O(1)$$, as it simply returns the stored value.\n\n- Space complexity:\n  - $$O(1)$$, as the operations modify the value in place without additional memory usage.\n\n# Code\n```typescript []\nclass Calculator {\n    private value = 0\n    \n    constructor(value: number) {\n        this.value = value\n    }\n    \n    add(value: number): Calculator {\n        this.value += value\n        return this\n    }\n    \n    subtract(value: number): Calculator {\n        this.value -= value\n        return this\n    }\n    \n    multiply(value: number): Calculator {\n        this.value *= value\n        return this\n    }\n    \n    divide(value: number): Calculator {\n        if (!value) throw \'Division by zero is not allowed\'\n\n        this.value /= value\n        return this\n    }\n    \n    power(value: number): Calculator {\n        this.value = Math.pow(this.value, value)\n        return this\n    }\n    \n    getResult(): number {\n        return this.value\n    }\n}\n```
['TypeScript', 'JavaScript']