2 files+11−10/2 viewed
| 14 | 14 | return a - b; | |
| 15 | 15 | } | |
| 16 | 16 | ||
| 17 | + | /** Returns a divided by b; throws a RangeError when b is 0. */ | |
| 18 | + | function divide(a, b) { | |
| 19 | + | if (b === 0) { | |
| 20 | + | throw new RangeError("Division by zero"); | |
| 21 | + | } | |
| 22 | + | return a / b; | |
| 23 | + | } | |
| 24 | + | ||
| 17 | 25 | /** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */ | |
| 18 | 26 | function power(base, exponent) { | |
| 19 | 27 | if (exponent < 0) { | |
| ⋯ | |||
| 59 | 67 | return Math.min(Math.max(value, min), max); | |
| 60 | 68 | } | |
| 61 | 69 | ||
| 62 | − | module.exports = { add, multiply, subtract, power, mean, median, standard_deviation, clamp }; | |
| 70 | + | module.exports = { add, multiply, subtract, divide, power, mean, median, standard_deviation, clamp }; | |
| 4 | 4 | ||
| 5 | 5 | assert.strictEqual(math.add(2, 3), 5); | |
| 6 | 6 | assert.strictEqual(math.multiply(4, 5), 20); | |
| 7 | + | assert.strictEqual(math.divide(10, 4), 2.5); | |
| 8 | + | assert.throws(() => math.divide(1, 0), RangeError); | |
| 7 | 9 | assert.strictEqual(math.power(2, 10), 1024); | |
| 8 | 10 | assert.strictEqual(math.power(5, 0), 1); | |
| 9 | 11 | assert.throws(() => math.power(2, -1), RangeError); |