Here's an implementation of a Chain Calculator in JavaScript. This calculator allows chaining operations like addition, subtraction, multiplication, and division in a fluent and readable way.
β Implementation
class ChainCalculator { constructor(initialValue = 0) { this.value = initialValue; } add(number) { this.value += number; return this; // Return `this` to enable chaining } subtract(number) { this.value -= number; return this; } multiply(number) { this.value *= number; return this; } divide(number) { if (number === 0) { throw new Error("Division by zero is not allowed."); } this.value /= number; return this; } getResult() { return this.value; } reset() { this.value = 0; return this; } } // Example usage: const calculator = new ChainCalculator(10); const result = calculator .add(5) // 10 + 5 = 15 .subtract(3) // 15 - 3 = 12 .multiply(4) // 12 * 4 = 48 .divide(2) // 48 / 2 = 24 .getResult(); console.log(result); // Output: 24
Explanation
- Constructor :
- Takes an initial value (default is
0).
- Methods :
add(number): Adds the given number to the current value.subtract(number): Subtracts the given number from the current value.multiply(number): Multiplies the current value by the given number.divide(number): Divides the current value by the given number, with a check to prevent division by zero.getResult(): Returns the current value.reset(): Resets the value to0(or any initial value).
- Chaining :
- Each method returns the instance (
this), enabling method chaining.
Try It
You can use this pattern to build more complex calculators by adding operations or features like exponentiation, modulus, or even memory storage.
Quick Quiz
Test your understanding with 3 quick questions
Q1What enables method chaining in the ChainCalculator class?
Q2What is the output of: `new ChainCalculator(10).add(5).multiply(2).getResult()`?
Q3What happens when you call `divide(0)` on the ChainCalculator?