{ "data": { "question": { "questionId": "2863", "questionFrontendId": "2726", "categoryTitle": "JavaScript", "boundTopicId": 2300583, "title": "Calculator with Method Chaining", "titleSlug": "calculator-with-method-chaining", "content": "
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.
\n
Example 1:
\n\n\nInput: \nactions = ["Calculator", "add", "subtract", "getResult"], \nvalues = [10, 5, 7]\nOutput: 8\nExplanation: \nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n\n\n
Example 2:
\n\n\nInput: \nactions = ["Calculator", "multiply", "power", "getResult"], \nvalues = [2, 5, 2]\nOutput: 100\nExplanation: \nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n\n\n
Example 3:
\n\n\nInput: \nactions = ["Calculator", "divide", "getResult"], \nvalues = [20, 0]\nOutput: "Division by zero is not allowed"\nExplanation: \nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\nThe error should be thrown because we cannot divide by zero.\n\n\n
\n
Constraints:
\n\nactions
is a valid JSON array of stringsvalues
is a valid JSON array of numbers2 <= actions.length <= 2 * 104
1 <= values.length <= 2 * 104 - 1
actions[i]
is one of "Calculator", "add", "subtract", "multiply", "divide", "power", and "getResult"设计一个类 Calculator
。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。Calculator
类的构造函数应接受一个数字作为 result
的初始值。
你的 Calculator
类应包含以下方法:
add
- 将给定的数字 value
与 result
相加,并返回更新后的 Calculator
对象。subtract
- 从 result
中减去给定的数字 value
,并返回更新后的 Calculator
对象。multiply
- 将 result
乘以给定的数字 value
,并返回更新后的 Calculator
对象。divide
- 将 result
除以给定的数字 value
,并返回更新后的 Calculator
对象。如果传入的值为 0
,则抛出错误 \"Division by zero is not allowed\"
。power
- 计算 result
的幂,指数为给定的数字 value
,并返回更新后的 Calculator
对象。(result = result ^ value
)getResult
- 返回 result
的值。结果与实际结果相差在 10-5
范围内的解被认为是正确的。
\n\n
示例 1:
\n\n\n输入:actions = [\"Calculator\", \"add\", \"subtract\", \"getResult\"], \nvalues = [10, 5, 7]\n输出:8\n解释:\nnew Calculator(10).add(5).subtract(7).getResult() // 10 + 5 - 7 = 8\n\n\n
示例 2:
\n\n\n输入:actions = [\"Calculator\", \"multiply\", \"power\", \"getResult\"], \nvalues = [2, 5, 2]\n输出:100\n解释:\nnew Calculator(2).multiply(5).power(2).getResult() // (2 * 5) ^ 2 = 100\n\n\n
示例 3:
\n\n\n输入:actions = [\"Calculator\", \"divide\", \"getResult\"], \nvalues = [20, 0]\n输出:\"Division by zero is not allowed\"\n解释:\nnew Calculator(20).divide(0).getResult() // 20 / 0 \n\n由于不能除以零,因此应抛出错误。\n\n\n
\n\n
提示:
\n\nactions
是一个有效的 JSON 字符串数组values
是一个有效的 JSON 字符串数组2 <= actions.length <= 2 * 104
1 <= values.length <= 2 * 104 - 1
actions[i]
是 \"Calculator\", \"add\", \"subtract\", \"multiply\", \"divide\", \"power\", 和 \"getResult\" 其中的元素\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]\n[\"Calculator\", \"multiply\", \"power\", \"getResult\"]\n[2, 5, 2]\n[\"Calculator\", \"divide\", \"getResult\"]\n[20, 0]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n