Skip to content
29 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 a minus b. */
13function subtract(a, b) {
14 return a - b;
15}
16
17/** Returns base raised to the whole-number exponent; throws a RangeError if exponent is negative. */
18function power(base, exponent) {
19 if (exponent < 0) {
20 throw new RangeError("exponent must not be negative");
21 }
22 let result = 1;
23 for (let i = 0; i < exponent; i++) {
24 result *= base;
25 }
26 return result;
27}
28
29module.exports = { add, multiply, subtract, power };