{ "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.

\n\n

Your Calculator class should have the following methods:

\n\n\n\n

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

\n\n

 

\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\n\n", "translatedTitle": "使用方法链的计算器", "translatedContent": "

设计一个类 Calculator 。该类应提供加法、减法、乘法、除法和乘方等数学运算功能。同时,它还应支持连续操作的方法链式调用。Calculator 类的构造函数应接受一个数字作为 result 的初始值。

\n\n

你的 Calculator 类应包含以下方法:

\n\n\n\n

结果与实际结果相差在 10-5 范围内的解被认为是正确的。

\n\n

 

\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\n\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 2, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "class Calculator {\n \n /** \n * @param {number} value\n */\n\tconstructor(value) {\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tadd(value){\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tsubtract(value){\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */ \n\tmultiply(value) {\n\t\t\n\t}\n\n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tdivide(value) {\n\t\t\n\t}\n \n /** \n * @param {number} value\n * @return {Calculator}\n */\n\tpower(value) {\n\t\t\n\t}\n \n /** \n * @return {number}\n */\n\tgetResult() {\n\t\t\n\t}\n}", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "class Calculator {\n \n\tconstructor(value: number) {\n\t\t\n }\n \n\tadd(value: number): Calculator {\n\t\t\n\t}\n \n\tsubtract(value: number): Calculator {\n\t\t\n\t}\n \n\tmultiply(value: number): Calculator {\n\t\t\n\t}\n\n\tdivide(value: number): Calculator {\n\t\t\n\t}\n \n\tpower(value: number): Calculator {\n\t\t\n\t}\n\n\tgetResult(): number {\n\t\t\n\t}\n}", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"1.9K\", \"totalSubmission\": \"3K\", \"totalAcceptedRaw\": 1949, \"totalSubmissionRaw\": 2977, \"acRate\": \"65.5%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "[\"Calculator\", \"add\", \"subtract\", \"getResult\"]\n[10, 5, 7]", "metaData": "{\n \"name\": \"foobar\",\n \"params\": [\n {\n \"name\": \"actions\",\n \"type\": \"string[]\"\n },\n {\n \"type\": \"double[]\",\n \"name\": \"values\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"javascript\":[\"JavaScript\",\"

\\u7248\\u672c\\uff1aNode.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n

\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a --harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/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>\"],\"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" } } }