2863. Calculator with Method Chaining
My accepted JavaScript solution to LeetCode problem 2863, Calculator with Method Chaining, running in 44ms.
- Difficulty: Easy
- JavaScript
- Runtime 44ms
- Memory 53.1MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
JavaScript
Accepted on LeetCode — runtime 44ms, memory 53.1MB, accepted 2025-12-31.
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 = Math.pow(this.result, value);
return this;
}
/**
* @return {number}
*/
getResult() {
return this.result;
}
}