Chained Calculator Implementation for Sequential Operations
Intuition\nMy initial thought was to create a Calculator class that could handle multiple operations in sequence, allowing each operation to be chained together
# Intuition\nMy initial thought was to create a Calculator class that could handle multiple operations in sequence, allowing each operation to be chained together. Since each action modifies a running total, I aimed to implement the class so that methods could be called consecutively on a single instance.\n\n# Approach\n1. Define the Calculator class: This class should hold the current result of calculations and implement methods for each operation (add, subtract, multiply, divide, power, getResult).\n2. Implement method chaining: Each method should modify the current result, then return this to allow chaining. The divide method includes a check to handle division by zero by setting the result to a specific error message.\n3. Use processActions function: This function initializes the Calculator with the first value in values, then iterates through actions to apply each specified operation with the corresponding value. The final result is obtained by calling getResult.\n\n# Complexity\n- Time complexity:\n \n\nO(n), where \n\uD835\uDC5B\nn is the number of operations. Each operation (like addition, subtraction, etc.) is a constant-time operation.\n\n- Space complexity:\nO(1), since only a fixed amount of space is used to store the result and intermediate values.\n\n# Code\n```javascript []\nclass Calculator {\n  constructor(initialValue) {\n    this.result = initialValue;\n  }\n\n  add(value) {\n    this.result += value;\n    return this; // Return this to allow method chaining\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      this.result = "Division by zero is not allowed";\n      return this;\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\nfunction processActions(actions, values) {\n  let calc = new Calculator(values[0]);\n  \n  for (let i = 1; i < actions.length; i++) {\n    let action = actions[i];\n    let value = values[i - 1]; // The corresponding value for the action\n\n    if (action === "add") {\n      calc.add(value);\n    } else if (action === "subtract") {\n      calc.subtract(value);\n    } else if (action === "multiply") {\n      calc.multiply(value);\n    } else if (action === "divide") {\n      calc.divide(value);\n    } else if (action === "power") {\n      calc.power(value);\n    }\n  }\n\n  return calc.getResult();\n}\n\n// Example usage:\nconsole.log(processActions(["Calculator", "add", "subtract", "getResult"], [10, 5, 7])); // Output: 8\nconsole.log(processActions(["Calculator", "multiply", "power", "getResult"], [2, 5, 2])); // Output: 100\nconsole.log(processActions(["Calculator", "divide", "getResult"], [20, 0])); // Output: "Division by zero is not allowed"\n\n```
['JavaScript']