diff --git a/README.md b/README.md index 9b5bca310..1e379163e 100644 --- a/README.md +++ b/README.md @@ -2142,7 +2142,10 @@ Returns the average of an of two or more numbers/arrays. Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array. ```js -const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) / arr.length; +const average = (...arr) => { + const nums = [].concat(...arr); + return nums.reduce((acc, val) => acc + val, 0) / nums.length; +}; ```
@@ -2150,6 +2153,7 @@ const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) ```js average([1, 2, 3]); // 2 +average(1, 2, 3); // 2 ```
diff --git a/docs/index.html b/docs/index.html index 8389e10d5..f2133ab4f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -407,8 +407,12 @@ runPromisesInSeries([() => delay(1000), () => delay(2000)]); // //executes

Logic

negate

Negates a predicate function.

Take a predicate function and apply not to it with its arguments.

const negate = func => (...args) => !func(...args);
 
filter([1, 2, 3, 4, 5, 6], negate(isEven)); // [1, 3, 5]
 negate(isOdd)(1); // false
-

Math

average

Returns the average of an of two or more numbers/arrays.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) / arr.length;
+

Math

average

Returns the average of an of two or more numbers/arrays.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...arr) => {
+  const nums = [].concat(...arr);
+  return nums.reduce((acc, val) => acc + val, 0) / nums.length;
+};
 
average([1, 2, 3]); // 2
+average(1, 2, 3); // 2
 

clampNumber

Clamps num within the inclusive range specified by the boundary values a and b.

If num falls within the range, return num. Otherwise, return the nearest number in the range.

const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));
 
clampNumber(2, 3, 5); // 3
 clampNumber(1, -1, -5); // -1