Jeff的隨手筆記

學習當一個前端工程師

0%

用LeetCode寫日記-Day30

最後一天了!!!雖然中間有兩天暫停了但還是完成了這個30天挑戰!讓我們把今天的學習完成吧

Day30: Calculator with Method Chaining

問題描述:

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.

問題難度:Easy

問題限制:

  • actions is a valid JSON array of strings
  • values is a valid JSON array of numbers
  • 2 <= actions.length <= 2 * 104
  • 1 <= values.length <= 2 * 104 - 1
  • actions[i] is one of “Calculator”, “add”, “subtract”, “multiply”, “divide”, “power”, and “getResult”
  • First action is always “Calculator”
  • Last action is always “getResult”

我的學習過程:

這題要做得是四則運算,感覺有些直覺。

首先先把這個值抓到:

1
2
3
constructor(value) {
this.result = value
}

Constructor 裡,我們接受了value這個值並把他設定成this.result 的初始值。

然後就是四則運算,但這邊要特別講一個我犯的錯誤:

我原本的寫法:

1
2
3
add(value) {
return this.result = this.result + value;
}

我的想法是做完運算後直接回傳做完結果,然後再進行下一次運算,但這時系統直接噴錯。

以我們的思考這樣確實沒錯,但在系統這邊卻是不行。

我們看一下這裡回傳this.resultthis的差別:

1
2
console.log(this) // Calculator { result: 10 }
console.log(this.result) // 10

看到這個結果我就知道我錯在哪裡了,我們的運算是用this.result = this.result + value;

但如果我們只回傳this.result 在我們進行下一次運算時執行到this.result 時就會找不到進而導致整個code執行錯誤。

因此我們必須回傳this :

1
2
3
4
add(value){
this.result = this.result + value
return this
}

之後的運算也都是用這樣的方式即可。

我的code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Calculator {

constructor(value) {
this.result = value
}

add(value){
this.result = this.result + value
return this
}

subtract(value){
this.result = this.result - value
return this
}

multiply(value) {
this.result = this.result * value
return this
}

divide(value) {
if (value === 0) {
throw new Error('Division by zero is not allowed')
}
this.result = this.result / value
return this
}

power(value) {
this.result = this.result ** value
return this
}

getResult() {
return this.result
}
}

完成感言:

一開始真的只想多練習JS,想說雖然該看的都看了但到底有沒有學會也不知道就來做題目。

在一開始決定做這個系列時我一直覺得自己應該會做的很厭世,甚至可能會中途放棄,誰知道越做覺得越有趣(遇到不會的還是很煩躁)。

在做題的過程中,真的還是有達到我的目標,因為發現真的很多東西知道要用什麼方式但是就是寫不出來,但至少比第一次來寫LeetCode時完全不知該如何下手好很多了。

接下來會先全力趕side project的進度以及整理這30天的筆記,等到這些都做完後會再回來繼續練習的,但這個可能要等到1月才能繼續開始了。