diff --git a/README.md b/README.md index 1e379163e..112aa68c6 100644 --- a/README.md +++ b/README.md @@ -2546,17 +2546,16 @@ isPrime(12); // false ### lcm -Returns the least common multiple of two or numbers/arrays. +Returns the least common multiple of two or more numbers/arrays. Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple. The GCD formula uses recursion. ```js const lcm = (...arr) => { - let data = [].concat(...arr); const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - const helperLcm = (x, y) => x * y / gcd(x, y); - return arr.reduce((a, b) => helperLcm(a, b)); + const _lcm = (x, y) => x * y / gcd(x, y); + return [].concat(...arr).reduce((a, b) => _lcm(a, b)); }; ``` diff --git a/docs/index.html b/docs/index.html index f2133ab4f..a45295e6e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -482,11 +482,10 @@ isArmstrongNumber(56); // false };
isPrime(11); // true
 isPrime(12); // false
-

lcm

Returns the least common multiple of two or numbers/arrays.

Use the greatest common divisor (GCD) formula and Math.abs() to determine the least common multiple. The GCD formula uses recursion.

const lcm = (...arr) => {
-  let data = [].concat(...arr);
+

lcm

Returns the least common multiple of two or more numbers/arrays.

Use the greatest common divisor (GCD) formula and Math.abs() to determine the least common multiple. The GCD formula uses recursion.

const lcm = (...arr) => {
   const gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  const helperLcm = (x, y) => x * y / gcd(x, y);
-  return arr.reduce((a, b) => helperLcm(a, b));
+  const _lcm = (x, y) => x * y / gcd(x, y);
+  return [].concat(...arr).reduce((a, b) => _lcm(a, b));
 };
 
lcm(12, 7); // 84
 lcm([1, 3, 4], 5); // 60