Talentd
FleetCode

2726. Calculator with Method Chaining

Easy76.2% acceptance2 companies

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"

Asked at 2 companies

6:22
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Calculator {
2
3 /**
4 * @param {number} value
5 */
6 constructor(value) {
7
8 }
9
10 /**
11 * @param {number} value
12 * @return {Calculator}
13 */
14 add(value){
15
16 }
17
18 /**
19 * @param {number} value
20 * @return {Calculator}
21 */
22 subtract(value){
23
24 }
25
26 /**
27 * @param {number} value
28 * @return {Calculator}
29 */
30 multiply(value) {
31
32 }
33
34 /**
35 * @param {number} value
36 * @return {Calculator}
37 */
38 divide(value) {
39
40 }
41
42 /**
43 * @param {number} value
44 * @return {Calculator}
45 */
46 power(value) {
47
48 }
49
50 /**
51 * @return {number}
52 */
53 getResult() {
54
55 }
56}
Starter codeJavaScriptSpaces: 4
Examples
Input
["Calculator", "add", "subtract", "getResult"]
[10, 5, 7]
Input
["Calculator", "multiply", "power", "getResult"]
[2, 5, 2]
Input
["Calculator", "divide", "getResult"]
[20, 0]
Your code is saved on this device.Solution