Skip to content

2726. Calculator with Method Chaining

Description

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

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:

  • actions is a valid JSON array of strings
  • valuesΒ is a valid JSON array of numbers
  • 2 <= actions.length <= 2 * 104
  • 1 <= values.length <= 2 * 104Β - 1
  • actions[i] is one of "Calculator", "add", "subtract", "multiply", "divide", "power", andΒ "getResult"
  • First action is always "Calculator"
  • Last action is always "getResult"

Solutions

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Calculator {
    private x: number;

    constructor(value: number) {
        this.x = value;
    }

    add(value: number): Calculator {
        this.x += value;
        return this;
    }

    subtract(value: number): Calculator {
        this.x -= value;
        return this;
    }

    multiply(value: number): Calculator {
        this.x *= value;
        return this;
    }

    divide(value: number): Calculator {
        if (value === 0) {
            throw new Error('Division by zero is not allowed');
        }
        this.x /= value;
        return this;
    }

    power(value: number): Calculator {
        this.x **= value;
        return this;
    }

    getResult(): number {
        return this.x;
    }
}

Comments