[Easy] LeetCode JS 30 - 2726. Calculator with Method Chaining

March 7, 2024

☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee

LeetCode 30 Days of JavaScript

This question is from LeetCode's 30 Days of JavaScript Challenge

2726. Calculator with Method Chaining

Question Prompt

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 number value to the result and returns the updated Calculator.
  • subtract - This method subtracts the given number value from the result and returns the updated Calculator.
  • multiply - This method multiplies the result by the given number value and returns the updated Calculator.
  • divide - This method divides the result by the given number value and returns the updated Calculator. If the passed value is 0, an error "Division by zero is not allowed" should be thrown.
  • power - This method raises the result to the power of the given number value and returns the updated Calculator.
  • getResult - This method returns the result.

Solutions within 10-5 of the actual result are considered correct.

// 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

Solutions

First, we define a class named Calculator. In the class, we have a constructor takes an value and assigns it to the this.value property. This this.value  property will store the intermediate and final results of our calculations.

Then we can add the arithmetic Methods (add, subtract, multiply, divide). Each arithmetic method modifies the this.value  property accordingly. Importantly, they all return this;. This is critical for method chaining.

Two methods to note in particular. divide method will check if the divisor (value) is zero. If it is, we throw an error to prevent division by zero. power method utilizes the built-in Math.pow function to calculate exponentiation.

class Calculator {
  constructor(value) {
    this.value = value;
  }

  add(value) {
    this.value += value;
    return this; // Enables method chaining
  }

  subtract(value) {
    this.value -= value;
    return this;
  }

  multiply(value) {
    this.value *= value;
    return this;
  }

  divide(value) {
    if (value === 0) {
      throw new Error("Division by zero is not allowed");
    }
    this.value /= value;
    return this;
  }

  power(value) {
    this.value = Math.pow(this.value, value);
    return this;
  }

  getResult() {
    return this.value;
  }
}
☕️ Support Us
Your support will help us to continue to provide quality content.👉 Buy Me a Coffee