|
| 1 | +/** |
| 2 | + * 2726. Calculator with Method Chaining |
| 3 | + * https://leetcode.com/problems/calculator-with-method-chaining/ |
| 4 | + * Difficulty: Easy |
| 5 | + * |
| 6 | + * Design a Calculator class. The class should provide the mathematical operations of addition, |
| 7 | + * subtraction, multiplication, division, and exponentiation. It should also allow consecutive |
| 8 | + * operations to be performed using method chaining. The Calculator class constructor should |
| 9 | + * accept a number which serves as the initial value of result. |
| 10 | + * |
| 11 | + * Your Calculator class should have the following methods: |
| 12 | + * - add - This method adds the given number value to the result and returns the updated Calculator. |
| 13 | + * - subtract - This method subtracts the given number value from the result and returns the |
| 14 | + * updated Calculator. |
| 15 | + * - multiply - This method multiplies the result by the given number value and returns the |
| 16 | + * updated Calculator. |
| 17 | + * - divide - This method divides the result by the given number value and returns the updated |
| 18 | + * Calculator. If the passed value is 0, an error "Division by zero is not allowed" should |
| 19 | + * be thrown. |
| 20 | + * - power - This method raises the result to the power of the given number value and returns |
| 21 | + * the updated Calculator. |
| 22 | + * - getResult - This method returns the result. |
| 23 | + * |
| 24 | + * Solutions within 10-5 of the actual result are considered correct. |
| 25 | + */ |
| 26 | + |
| 27 | +class Calculator { |
| 28 | + /** |
| 29 | + * @param {number} value |
| 30 | + */ |
| 31 | + constructor(value) { |
| 32 | + this.value = value; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param {number} value |
| 37 | + * @return {Calculator} |
| 38 | + */ |
| 39 | + add(value) { |
| 40 | + this.value += value; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param {number} value |
| 46 | + * @return {Calculator} |
| 47 | + */ |
| 48 | + subtract(value) { |
| 49 | + this.value -= value; |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param {number} value |
| 55 | + * @return {Calculator} |
| 56 | + */ |
| 57 | + multiply(value) { |
| 58 | + this.value *= value; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param {number} value |
| 64 | + * @return {Calculator} |
| 65 | + */ |
| 66 | + divide(value) { |
| 67 | + if (value === 0) { |
| 68 | + throw new Error('Division by zero is not allowed'); |
| 69 | + } |
| 70 | + this.value /= value; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param {number} value |
| 76 | + * @return {Calculator} |
| 77 | + */ |
| 78 | + power(value) { |
| 79 | + this.value **= value; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return {number} |
| 85 | + */ |
| 86 | + getResult() { |
| 87 | + return this.value; |
| 88 | + } |
| 89 | +} |
0 commit comments