Calculator with Method Chaining
LeetCode題目: 2726. Calculator with Method Chaining
My solution:
class Calculator {
/**
* @param {number} value
*/
constructor(value) {
this.digit = Number(value);
}
/**
* @param {number} value
* @return {Calculator}
*/
add(value){
this.digit += value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
subtract(value){
this.digit -= value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
multiply(value) {
this.digit *= value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
divide(value) {
(value == 0)
? this.digit = 'Division by zero is not allowed'
: this.digit /= value;
return this;
}
/**
* @param {number} value
* @return {Calculator}
*/
power(value) {
this.digit = Math.pow(this.digit, value);
return this;
}
/**
* @return {number}
*/
getResult() {
return this.digit;
}
}