2025-08-21 16:06:09 +0300 MSK

Calculator with Method Chaining

Code

class Calculator {
    
    /** 
     * @param {number} value
     */
    constructor(value) {
        this.result = value 
    }
    
    /** 
     * @param {number} value
     * @return {Calculator}
     */
    add(value){
        this.result += value 
        return this 
    }
    
    /** 
     * @param {number} value
     * @return {Calculator}
     */
    subtract(value){
        this.result -= value 
        return this 
    }
    
    /** 
     * @param {number} value
     * @return {Calculator}
     */  
    multiply(value) {
        this.result *= value 
        return this 
    }
    
    /** 
     * @param {number} value
     * @return {Calculator}
     */
    divide(value) {

   if(value === 0){
     throw new Error("Division by zero is not allowed")
        }
        this.result /= value 
        return this 
    }
    
    /** 
     * @param {number} value
     * @return {Calculator}
     */
    power(value) {
        this.result **= value 
        return this 
    }
    
    /** 
     * @return {number}
     */
    getResult() {
        return this.result 
    }
}