Skip to content
32 linesCodeBlameRaw
1// Small helpers for numbers.
2/** Returns the sum of a and b. */
3function add(a, b) {
4 return a + b;
5}
6
7/** Returns the product of a and b. */
8function multiply(a, b) {
9 return a * b;
10}
11
12/** 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
20/** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */
21function power(base, exponent) {
22 if (exponent < 0) {
23 throw new RangeError("exponent must not be negative");
24 }
25 let result = 1;
26 for (let i = 0; i < exponent; i++) {
27 result *= base;
28 }
29 return result;
30}
31
32module.exports = { add, multiply, mod, power };