Merge main into mod branch, keeping divide and mod
2 files+11−10/2 viewed
| 22 | 22 | return a - b; | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | + | /** Returns a divided by b; throws a RangeError when b is 0. */ | |
| 26 | + | function divide(a, b) { | |
| 27 | + | if (b === 0) { | |
| 28 | + | throw new RangeError("Division by zero"); | |
| 29 | + | } | |
| 30 | + | return a / b; | |
| 31 | + | } | |
| 32 | + | ||
| 25 | 33 | /** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */ | |
| 26 | 34 | function power(base, exponent) { | |
| 27 | 35 | if (exponent < 0) { | |
| ⋯ | |||
| 59 | 67 | return Math.sqrt(variance); | |
| 60 | 68 | } | |
| 61 | 69 | ||
| 62 | − | module.exports = { add, multiply, subtract, mod, power, mean, median, standard_deviation }; | |
| 70 | + | module.exports = { add, multiply, subtract, divide, mod, power, mean, median, standard_deviation }; | |
| 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.mod(7, 3), 1); | |
| 8 | 10 | assert.strictEqual(math.mod(-1, 3), 2); | |
| 9 | 11 | assert.strictEqual(math.mod(6, 3), 0); |