Calculator with Method Chaining
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Design a Calculator class. The class should provide the mathematical operations of addition, subtraction, multiplication, division, and exponentiation. It should also allow consecutive operations to be performed using method chaining. The Calculator class constructor should accept a number which serves as the initial value of result.
Your Calculator class should have the following methods:
add- This method adds the given numbervalueto theresultand returns the updatedCalculator.subtract- This method subtracts the given numbervaluefrom theresultand returns the updatedCalculator.multiply- This method multiplies theresultby the given numbervalueand returns the updatedCalculator.divide- This method divides theresultby the given numbervalueand returns the updatedCalculator. If the passed value is0, an error"Division by zero is not allowed"should be thrown.power- This method raises theresultto the power of the given numbervalueand returns the updatedCalculator.getResult- This method returns theresult.
Solutions within 10-5 of the actual result are considered correct.
Examples
Example 1
Input:
actions = ["Calculator", "add", "subtract", "getResult"],
values = [10, 5, 7]
Output: 8
Explanation:
new Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8
Example 2
Input:
actions = ["Calculator", "multiply", "power", "getResult"],
values = [2, 5, 2]
Output: 100
Explanation:
new Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100
Example 3
Input:
actions = ["Calculator", "divide", "getResult"],
values = [20, 0]
Output: "Division by zero is not allowed"
Explanation:
new Calculator(20).divide(0).getResult() // 20 / 0
The error should be thrown because we cannot divide by zero.
Constraints
actionsis a valid JSON array of stringsvaluesis a valid JSON array of numbers2 <= actions.length <= 2 * 10^41 <= values.length <= 2 * 104 - 1actions[i]is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"- First action is always "Calculator"
- Last action is always "getResult"
Solution
Method 1 – Method Chaining with Error Handling
Intuition
We use a class to store the result and return this from each operation to allow chaining. For division, we throw an error if dividing by zero. The result is always updated in place, and getResult returns the current value.
Approach
- Store the result as a property in the constructor.
- Each method (
add,subtract,multiply,divide,power) updates the result and returnsthisfor chaining. - In
divide, throw an error if the value is 0. getResultreturns the result.
Code
JavaScript
class Calculator {
constructor(result) {
this.result = result;
}
add(val) {
this.result += val;
return this;
}
subtract(val) {
this.result -= val;
return this;
}
multiply(val) {
this.result *= val;
return this;
}
divide(val) {
if (val === 0) throw new Error("Division by zero is not allowed");
this.result /= val;
return this;
}
power(val) {
this.result = Math.pow(this.result, val);
return this;
}
getResult() {
return this.result;
}
}
TypeScript
class Calculator {
private result: number;
constructor(result: number) {
this.result = result;
}
add(val: number): this {
this.result += val;
return this;
}
subtract(val: number): this {
this.result -= val;
return this;
}
multiply(val: number): this {
this.result *= val;
return this;
}
divide(val: number): this {
if (val === 0) throw new Error("Division by zero is not allowed");
this.result /= val;
return this;
}
power(val: number): this {
this.result = Math.pow(this.result, val);
return this;
}
getResult(): number {
return this.result;
}
}
Complexity
- ⏰ Time complexity: O(1) per operation
- 🧺 Space complexity: O(1)