iT邦幫忙

2023 iThome 鐵人賽

DAY 30
0
Software Development

LeetCode-30 Days of JavaScript系列 第 30

LeetCode JS30-Day30 | 2726. Calculator with Method Chaining 使用方法鍊的計算機

  • 分享至 

  • xImage
  •  

LeetCode JS30-Day30 | 2726. Calculator with Method Chaining Easy

Description❓

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⁻⁵ of the actual result are considered correct.

設計一個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
  • getResult - 此方法回傳result
    實際結果「10⁻⁵」以內的解被認為是正確的。

Points

Solution✍️

[ ▶️挑戰這一題 ][ 本日代碼 ]

class Calculator{
    //建構函式 初始化屬性result=參數的val值
    constructor(val){ this.result = val;}
    //加法
    add(val){
        this.result += val;
        return this;
    }
    //減法
    subtract(val){
        this.result -= val;
        return this;
    }
    //乘法
    multiply(val){
        this.result *= val;
        return this;        
    }   
    //除法
    divide(val){
        if (val == 0){ 
            throw new Error(
                "Division by zero is not allowed"
        )}
        this.result /= val;
        return this;
    }   
    //指數運算(幂)
    power(val){
        this.result **= val;
        return this;
    }  
    //取得結果
    getResult(val){
        return this.result;
    }
}

Testcase

const cal = new Calculator(10);
let ans = cal.add(5).subtract(7).getResult();
console.log(`[Test1] ${ans}`);
//Output: 8
const cal2 = new Calculator(2);
ans = cal2.multiply(5).power(2).getResult();
console.log(`[Test2] ${ans}`);
//Output: 100
const cal3 = new Calculator(20);
ans = cal3.divide(0).getResult();
console.log(`[Test3] ${ans}`);
//Output: Uncaught Error Error: Division by zero is not allowed

上一篇
LeetCode JS30-Day29 | 2695. Array Wrapper 包裝陣列
系列文
LeetCode-30 Days of JavaScript30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言