Method Chaining: Calculator

Easy

Prompt

JavaScript is a versatile language and allows for a variety of design patterns. One pattern that can lead to clean, readable code is method chaining.

Your task is to implement a MathCalculator class that supports method chaining. The MathCalculator should accept an optional initial value in its constructor. If no initial value is provided, it should default to 0.

The divide method should throw an error when trying to divide by zero.

Requirements

The class should support the following methods:

  • add(number): Adds the given number to the current total.
  • subtract(number): Subtracts the given number from the current total.
  • multiply(number): Multiplies the current total by the given number.
  • divide(number): Divides the current total by the given number.
  • getValue(): Returns the current total.

Example

let calculator = new MathCalculator(10);
let result = calculator
.add(10)
.multiply(3)
.subtract(5)
.divide(2)
.getValue();

console.log(result); // Expected output: 22.5

Playground

Hint 1

Begin by creating a class named MathCalculator. In the constructor, initialize an instance variable to store the calculator's current value. You might start with an initial value passed as an argument to the constructor or default to 0 if no value is provided.

Hint 2

For each arithmetic function like add, subtract, multiply, and divide, implement a method in the class. These methods should update the calculator's value based on the operation. For example, in the add method, increase the current value by the number passed as an argument. Remember to return this at the end of each method to enable method chaining.

Solution

Explanation

The Calculator class in JavaScript allows you to perform mathematical operations using method chaining. The magic of this chaining happens because each method in the class except getValue() returns the instance of the class itself this.

Here's what happens step-by-step:

  • When you create a new calculator, the class constructor sets the initial value. For example, let calculator = new MathCalculator(10); starts the calculator with a value of 10.
  • Then, when you call a method like add(10), it adds 10 to the current value (which was initially 10), making it 20. At the end of this method, it returns this - the calculator instance itself.
  • Because add(10) returned the calculator instance, you can call another method on the result - like multiply(3). This takes the current value (20), multiplies it by 3, making it 60, and then again returns the calculator instance.
  • This chain can continue as long as you want because each method returns the calculator instance. In the end, when you're done chaining and want the result, you call getValue(). This method simply returns the current value.
  • If any operation is not possible (like dividing by zero), an error is thrown.

In simple terms, think of this calculator as a clever machine that does a job, then immediately hands itself back to you, ready to do the next job!

00:00