自助网站建设开发,网站百度地图标注,加拿大pc网站搭建,深圳网站建设微赢天下题目来源#xff1a; leetcode题目#xff0c;网址#xff1a;2726. 使用方法链的计算器 - 力扣#xff08;LeetCode#xff09;
解题思路#xff1a; 按要求模拟#xff0c;在计算后返回自己以达到链式调用的目的。
解题代码#xff1a;
class Calculator {/**…题目来源 leetcode题目网址2726. 使用方法链的计算器 - 力扣LeetCode
解题思路 按要求模拟在计算后返回自己以达到链式调用的目的。
解题代码
class Calculator {/** * param {number} value*/constructor(value) {this.resvalue;}/** * param {number} value* return {Calculator}*/add(value){this.resvalue;return this;}/** * param {number} value* return {Calculator}*/subtract(value){this.res-value;return this;}/** * param {number} value* return {Calculator}*/ multiply(value) {this.res*value;return this;}/** * param {number} value* return {Calculator}*/divide(value) {if(value0){throw Division by zero is not allowed;}else{this.resthis.res/value;}return this;}/** * param {number} value* return {Calculator}*/power(value) {var tempthis.res;if(value0){temp1/temp;value-1*value;}this.res1;for(var i0;ivalue;i){this.resthis.res*temp;}return this;}/** * return {number}*/getResult() {return this.res;}
} 总结 无官方题解。 ** 幂赋值a**b计算 a 的 b 次方并将结果赋给 a。