| 1 | // Small helpers for numbers. |
| 2 | /** Returns the sum of a and b. */ |
| 3 | function add(a, b) { |
| 4 | return a + b; |
| 5 | } |
| 6 | |
| 7 | /** Returns the product of a and b. */ |
| 8 | function multiply(a, b) { |
| 9 | return a * b; |
| 10 | } |
| 11 | |
| 12 | /** Returns a divided by b; throws a RangeError when b is 0. */ |
| 13 | function divide(a, b) { |
| 14 | if (b === 0) { |
| 15 | throw new RangeError("Division by zero"); |
| 16 | } |
| 17 | return a / b; |
| 18 | } |
| 19 | |
| 20 | /** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */ |
| 21 | function power(base, exponent) { |
| 22 | if (exponent < 0) { |
| 23 | throw new RangeError("exponent must not be negative"); |
| 24 | } |
| 25 | let result = 1; |
| 26 | for (let i = 0; i < exponent; i++) { |
| 27 | result *= base; |
| 28 | } |
| 29 | return result; |
| 30 | } |
| 31 | |
| 32 | module.exports = { add, multiply, divide, power }; |