{ "data": { "question": { "questionId": "2863", "questionFrontendId": "2726", "boundTopicId": null, "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": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 84, "dislikes": 13, "isLiked": null, "similarQuestions": "[]", "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]", "categoryTitle": "JavaScript", "contributors": [], "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\": \"12K\", \"totalSubmission\": \"16.4K\", \"totalAcceptedRaw\": 12040, \"totalSubmissionRaw\": 16393, \"acRate\": \"73.4%\"}", "hints": [], "solution": { "id": "1994", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "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, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 5.1.6, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2022 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }