3 files+83−10/3 viewed
| 1 | 1 | # automation lab | |
| 2 | + | ||
| 3 | + | ## Functions | |
| 4 | + | ||
| 5 | + | All functions are exported from `math.js`. | |
| 6 | + | ||
| 7 | + | ```js | |
| 8 | + | const { add, multiply, power, mean, median, standard_deviation } = require("./math"); | |
| 9 | + | ``` | |
| 10 | + | ||
| 11 | + | - `add(a, b)`: returns the sum of `a` and `b`. | |
| 12 | + | ```js | |
| 13 | + | add(2, 3); // 5 | |
| 14 | + | ``` | |
| 15 | + | - `multiply(a, b)`: returns the product of `a` and `b`. | |
| 16 | + | ```js | |
| 17 | + | multiply(4, 5); // 20 | |
| 18 | + | ``` | |
| 19 | + | - `power(base, exponent)`: returns `base` raised to a whole-number `exponent`; throws a `RangeError` if `exponent` is negative. | |
| 20 | + | ```js | |
| 21 | + | power(2, 3); // 8 | |
| 22 | + | ``` | |
| 23 | + | - `mean(numbers)`: returns the average of an array of numbers; throws a `TypeError` if the array is empty. | |
| 24 | + | ```js | |
| 25 | + | mean([1, 2, 3, 4]); // 2.5 | |
| 26 | + | ``` | |
| 27 | + | - `median(numbers)`: returns the middle value (the average of the two middle values when the length is even); throws a `TypeError` if the array is empty. | |
| 28 | + | ```js | |
| 29 | + | median([3, 1, 2]); // 2 | |
| 30 | + | median([1, 2, 3, 4]); // 2.5 | |
| 31 | + | ``` | |
| 32 | + | - `standard_deviation(numbers)`: returns the population standard deviation; throws a `TypeError` if the array is empty. | |
| 33 | + | ```js | |
| 34 | + | standard_deviation([2, 4, 4, 4, 5, 5, 7, 9]); // 2 | |
| 35 | + | ``` | |
| 36 | + | ||
| 2 | 37 | Agent runs here have a filtered network: package registries and g1t only. |
| 9 | 9 | return a * b; | |
| 10 | 10 | } | |
| 11 | 11 | ||
| 12 | − | module.exports = { add, multiply }; | |
| 12 | + | /** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */ | |
| 13 | + | function power(base, exponent) { | |
| 14 | + | if (exponent < 0) { | |
| 15 | + | throw new RangeError("exponent must not be negative"); | |
| 16 | + | } | |
| 17 | + | let result = 1; | |
| 18 | + | for (let i = 0; i < exponent; i++) { | |
| 19 | + | result *= base; | |
| 20 | + | } | |
| 21 | + | return result; | |
| 22 | + | } | |
| 23 | + | ||
| 24 | + | /** Returns the average of numbers; throws a TypeError if the array is empty. */ | |
| 25 | + | function mean(numbers) { | |
| 26 | + | if (numbers.length === 0) { | |
| 27 | + | throw new TypeError("numbers must not be empty"); | |
| 28 | + | } | |
| 29 | + | return numbers.reduce((sum, n) => sum + n, 0) / numbers.length; | |
| 30 | + | } | |
| 31 | + | ||
| 32 | + | /** Returns the middle value of numbers (the average of the two middle values when the length is even); throws a TypeError if the array is empty. */ | |
| 33 | + | function median(numbers) { | |
| 34 | + | if (numbers.length === 0) { | |
| 35 | + | throw new TypeError("numbers must not be empty"); | |
| 36 | + | } | |
| 37 | + | const sorted = [...numbers].sort((a, b) => a - b); | |
| 38 | + | const mid = Math.floor(sorted.length / 2); | |
| 39 | + | return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid]; | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | /** Returns the population standard deviation of numbers; throws a TypeError if the array is empty. */ | |
| 43 | + | function standard_deviation(numbers) { | |
| 44 | + | const avg = mean(numbers); | |
| 45 | + | const variance = numbers.reduce((sum, n) => sum + (n - avg) ** 2, 0) / numbers.length; | |
| 46 | + | return Math.sqrt(variance); | |
| 47 | + | } | |
| 48 | + | ||
| 49 | + | module.exports = { add, multiply, power, mean, median, standard_deviation }; |
| 3 | 3 | ||
| 4 | 4 | assert.strictEqual(math.add(2, 3), 5); | |
| 5 | 5 | assert.strictEqual(math.multiply(4, 5), 20); | |
| 6 | + | assert.strictEqual(math.power(2, 10), 1024); | |
| 7 | + | assert.strictEqual(math.power(5, 0), 1); | |
| 8 | + | assert.throws(() => math.power(2, -1), RangeError); | |
| 9 | + | assert.strictEqual(math.mean([1, 2, 3, 4]), 2.5); | |
| 10 | + | assert.strictEqual(math.median([3, 1, 2]), 2); | |
| 11 | + | assert.strictEqual(math.median([4, 1, 3, 2]), 2.5); | |
| 12 | + | assert.strictEqual(math.standard_deviation([2, 4, 4, 4, 5, 5, 7, 9]), 2); | |
| 13 | + | for (const fn of [math.mean, math.median, math.standard_deviation]) { | |
| 14 | + | assert.throws(() => fn([]), TypeError); | |
| 15 | + | } | |
| 6 | 16 | console.log("all tests passed"); |