diff --git a/README.md b/README.md index 162d88509..233609b8e 100644 --- a/README.md +++ b/README.md @@ -2676,11 +2676,11 @@ inrange(3, 2); // false Checks if the given number is an Armstrong number or not. -Convert the given number into an array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. +Convert the given number into an array of digits. Use the exponent operator (`**`) to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`. ```js const isArmstrongNumber = digits => - (arr => arr.reduce((a, d) => a + Math.pow(parseInt(d), arr.length), 0) == digits)( + (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)( (digits + '').split('') ); ``` @@ -3024,9 +3024,7 @@ You can omit the second argument to get the sample standard deviation or set it const standardDeviation = (arr, usePopulation = false) => { const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length; return Math.sqrt( - arr - .reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), []) - .reduce((acc, val) => acc + val, 0) / + arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1)) ); }; diff --git a/docs/index.html b/docs/index.html index ed310daf9..a82583589 100644 --- a/docs/index.html +++ b/docs/index.html @@ -532,8 +532,8 @@ elo([1200, 1200], 64); // [1232, 1168] inRange(3, 4); // true inRange(2, 3, 5); // false inrange(3, 2); // false -

isArmstrongNumber

Checks if the given number is an Armstrong number or not.

Convert the given number into an array of digits. Use Math.pow() to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return true otherwise false.

const isArmstrongNumber = digits =>
-  (arr => arr.reduce((a, d) => a + Math.pow(parseInt(d), arr.length), 0) == digits)(
+

isArmstrongNumber

Checks if the given number is an Armstrong number or not.

Convert the given number into an array of digits. Use the exponent operator (**) to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return true otherwise false.

const isArmstrongNumber = digits =>
+  (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(
     (digits + '').split('')
   );
 
isArmstrongNumber(1634); // true
@@ -590,9 +590,7 @@ median([0, 10, -2, 7]); // 3.5
 

standardDeviation

Returns the standard deviation of an array of numbers.

Use Array.reduce() to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then determine the standard deviation. You can omit the second argument to get the sample standard deviation or set it to true to get the population standard deviation.

const standardDeviation = (arr, usePopulation = false) => {
   const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
   return Math.sqrt(
-    arr
-      .reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
-      .reduce((acc, val) => acc + val, 0) /
+    arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc + val, 0) /
       (arr.length - (usePopulation ? 0 : 1))
   );
 };