Skip to content
62 linesCodeBlameRaw

Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.

Add math helpers, tests and checks1// Small helpers for numbers.
Merge #3: Add multiply to math.js2/** Returns the sum of a and b. */
Add math helpers, tests and checks3function add(a, b) {
4 return a + b;
5}
6
Merge #3: Add multiply to math.js7/** Returns the product of a and b. */
8function multiply(a, b) {
9 return a * b;
10}
11
Add mod function to math.js12/** Returns the non-negative remainder of a divided by b; throws RangeError when b is 0. */
13function mod(a, b) {
14 if (b === 0) {
15 throw new RangeError("mod by zero");
16 }
17 return ((a % b) + Math.abs(b)) % Math.abs(b);
18}
19
Merge main into mod branch, keeping subtract and mod20/** Returns a minus b. */
21function subtract(a, b) {
22 return a - b;
23}
24
Catch up with main25/** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */
26function power(base, exponent) {
27 if (exponent < 0) {
28 throw new RangeError("exponent must not be negative");
29 }
30 let result = 1;
31 for (let i = 0; i < exponent; i++) {
32 result *= base;
33 }
34 return result;
35}
36
Catch up with main37/** Returns the average of numbers; throws a TypeError if the array is empty. */
38function mean(numbers) {
39 if (numbers.length === 0) {
40 throw new TypeError("numbers must not be empty");
41 }
42 return numbers.reduce((sum, n) => sum + n, 0) / numbers.length;
43}
44
45/** 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. */
46function median(numbers) {
47 if (numbers.length === 0) {
48 throw new TypeError("numbers must not be empty");
49 }
50 const sorted = [...numbers].sort((a, b) => a - b);
51 const mid = Math.floor(sorted.length / 2);
52 return sorted.length % 2 === 0 ? (sorted[mid - 1] + sorted[mid]) / 2 : sorted[mid];
53}
54
55/** Returns the population standard deviation of numbers; throws a TypeError if the array is empty. */
56function standard_deviation(numbers) {
57 const avg = mean(numbers);
58 const variance = numbers.reduce((sum, n) => sum + (n - avg) ** 2, 0) / numbers.length;
59 return Math.sqrt(variance);
60}
61
Merge main into mod branch, keeping subtract and mod62module.exports = { add, multiply, subtract, mod, power, mean, median, standard_deviation };

This file's history is long; its oldest lines are credited to the oldest commit read.