Skip to content

Commit

Add mod function to math.js

g1tbot and Claudeagentcommitted Parent0ae9018Browse files
2 files+14−10/2 viewed
+9−1
99 return a * b;
1010 }
1111
12−module.exports = { add, multiply };
12+/** Returns the non-negative remainder of a divided by b; throws RangeError when b is 0. */
13+function 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+
20+module.exports = { add, multiply, mod };
+5−0
33
44 assert.strictEqual(math.add(2, 3), 5);
55 assert.strictEqual(math.multiply(4, 5), 20);
6+assert.strictEqual(math.mod(7, 3), 1);
7+assert.strictEqual(math.mod(-1, 3), 2);
8+assert.strictEqual(math.mod(6, 3), 0);
9+assert.strictEqual(math.mod(-1, -3), 2);
10+assert.throws(() => math.mod(1, 0), RangeError);
611 console.log("all tests passed");