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;
    }
}
profile-image
Hi, 我是 Zeki。目前為一名前端工程師。我相信科技始終來自於人性,是用來幫助人們過上更有品質的生活的,但願也希望如此。