{ "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 * 1041 <= values.length <= 2 * 104 - 1actions[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 * 1041 <= values.length <= 2 * 104 - 1actions[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 \\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js <\\/a>\\u5e93\\u6240\\u63d0\\u4f9b\\u7684\\u6570\\u636e\\u7ed3\\u6784\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.7.3<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2024<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\nNode.js 22.14.0<\\/code><\\/p>\\r\\n\\r\\n--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n