SIMPLE TO UNDERSTAND | THE EASIEST SOLUTION 🎯💯❤️‍🔥🏆🥇 JAVASCRIPT ✅🕶🤏📚 🧠 🎓
2726. Calculator with Method Chaining\n\n\uD83C\uDF31 Difficulty: Easy\n\n\uD83D\uDC81\uD83C\uDFFB\u200D\u2640\uFE0F All my solved LeetCode problems on GitHub: 
# 2726. Calculator with Method Chaining\n\n\uD83C\uDF31 Difficulty: `Easy`\n\n\uD83D\uDC81\uD83C\uDFFB\u200D\u2640\uFE0F All my solved LeetCode problems on GitHub: [rayanasales/leetcode](https://github.com/rayanasales/leetcode)\n\n\u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25 If it\'s help, please up \uD83D\uDD1D vote! \u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25\u2764\uFE0F\u200D\uD83D\uDD25\n\n---\n\n# Solution\n\n```Javascript []\nclass Calculator {\n  constructor(value) {\n    this.result = value;\n  }\n\n  add(value) {\n    this.result += value;\n    return this;\n  }\n\n  subtract(value) {\n    this.result -= value;\n    return this;\n  }\n\n  multiply(value) {\n    this.result *= value;\n    return this;\n  }\n\n  divide(value) {\n    if (value === 0) {\n      throw new Error("Division by zero is not allowed");\n    }\n    this.result /= value;\n    return this;\n  }\n\n  power(value) {\n    this.result = Math.pow(this.result, value);\n    return this;\n  }\n\n  getResult() {\n    return this.result;\n  }\n}\n```\n\n# Explanation\n\n### Code Explanation\n\n1. **Constructor**:\n\n   - The `constructor` initializes the `Calculator` instance with an initial `result` value.\n   - `this.result = value;` sets the initial value of `result` to the value passed when an instance is created.\n\n2. **Add Method**:\n\n   - `add(value)` method adds the passed `value` to the current `result`.\n   - It then returns `this`, allowing for method chaining. `this` refers to the current instance of the class.\n\n3. **Subtract Method**:\n\n   - Similar to `add`, `subtract(value)` subtracts the passed `value` from `result`.\n   - Returns `this` for continued chaining.\n\n4. **Multiply Method**:\n\n   - `multiply(value)` multiplies the `result` by the passed `value`.\n   - Again, it returns `this` to enable method chaining.\n\n5. **Divide Method**:\n\n   - In `divide(value)`, first, there\'s a check to ensure that division by zero does not occur.\n   - If `value` is `0`, it throws an `Error`, stopping execution and notifying the user.\n   - Otherwise, it divides the `result` by `value` and returns `this`.\n\n6. **Power Method**:\n\n   - `power(value)` uses `Math.pow` to raise `result` to the power of `value`.\n   - Returns `this` for chaining.\n\n7. **Get Result Method**:\n   - `getResult()` simply returns the current value of `result`.\n   - This method ends the chaining by returning a primitive value (the result of computations).\n\n### How Method Chaining Works\n\nMethod chaining is possible because most methods in the `Calculator` class return `this`, which is the instance of the class. By returning `this`, you can call another method on the same instance immediately. For example, `new Calculator(10).add(5).subtract(7).getResult()` seamlessly performs several operations in one line.\n\nThis design pattern is known as the **Fluent Interface** and is commonly used in JavaScript to create more readable code. By enabling method chaining, you make the syntax more expressive and closer to natural language, which enhances readability and ease of use.\n\n---\n\n# Please UPVOTE if this was helpful \uD83D\uDD1D\uD83D\uDD1D\uD83D\uDD1D\u2764\uFE0F\u2764\uFE0F\u2764\uFE0F\n\nand check it out all my solved LeetCode problems on GitHub: [rayanasales/leetcode](https://github.com/rayanasales/leetcode) \uD83E\uDD19\uD83D\uDE1A\uD83E\uDD18\n\n![image.png](https://assets.leetcode.com/users/images/57bce3b1-56e2-4c20-9cdf-b61fef26b93b_1725494158.6252415.png)\n
['Math', 'JavaScript']