diff --git a/README.md b/README.md index bde7d8eff..76f4f0ded 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,6 @@
View contents -* [`arrayGcd`](#arraygcd) -* [`arrayLcm`](#arraylcm) * [`arrayMax`](#arraymax) * [`arrayMin`](#arraymin) * [`chunk`](#chunk) @@ -427,61 +425,6 @@ arrayMax([1, 2, 4]); // 4 ## Array -### arrayGcd - -Calculates the greatest common denominator (gcd) of an array of numbers. - -Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the greatest common denominator of an array of numbers. - -```js -const arrayGcd = arr => { - const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - return arr.reduce((a, b) => gcd(a, b)); -}; -``` - -
-Examples - -```js -arrayGcd([1, 2, 3, 4, 5]); // 1 -arrayGcd([4, 8, 12]); // 4 -``` - -
- - -[⬆ Back to top](#table-of-contents) - - -### arrayLcm - -Calculates the lowest common multiple (lcm) of an array of numbers. - -Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the lowest common multiple of an array of numbers. - -```js -const arrayLcm = arr => { - const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - const lcm = (x, y) => x * y / gcd(x, y); - return arr.reduce((a, b) => lcm(a, b)); -}; -``` - -
-Examples - -```js -arrayLcm([1, 2, 3, 4, 5]); // 60 -arrayLcm([4, 8, 12]); // 24 -``` - -
- - -[⬆ Back to top](#table-of-contents) - - ### arrayMax Returns the maximum value in an array. @@ -2567,14 +2510,18 @@ fibonacciCountUntilNum(10); // 7 ### gcd -Calculates the greatest common divisor between two numbers. +Calculates the greatest common divisor between two or more numbers/arrays. -Use recursion. +The `helperGcd `function uses recursion. Base case is when `y` equals `0`. In this case, return `x`. Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js -const gcd = (x, y) => (!y ? x : gcd(y, x % y)); +const gcd = (...arr) => { + let data = [].concat(...arr); + const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); + return data.reduce((a, b) => helperGcd(a, b)); +}; ```
@@ -2750,15 +2697,17 @@ isPrime(12); // false ### lcm -Returns the least common multiple of two numbers. +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. ```js -const lcm = (x, y) => { +const lcm = (...arr) => { + let data = [].concat(...arr); const gcd = (x, y) => (!y ? x : gcd(y, x % y)); - return Math.abs(x * y) / gcd(x, y); + const helperLcm = (x, y) => x * y / gcd(x, y); + return arr.reduce((a, b) => helperLcm(a, b)); }; ``` @@ -2767,6 +2716,7 @@ const lcm = (x, y) => { ```js lcm(12, 7); // 84 +lcm([1, 3, 4], 5); // 60 ```
diff --git a/docs/index.html b/docs/index.html index 9fbd42386..46e60878d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -107,9 +107,7 @@ spreadOver

Array -

arrayGcd -arrayLcm -arrayMax +arrayMax arrayMin chunk compact @@ -342,30 +340,7 @@ arrayMax([1, 2, 3]); // 3 arrayMax([1, 2, 4]); // 4

Array

-

arrayGcd

-

Calculates the greatest common denominator (gcd) of an array of numbers.

-

Use Array.reduce() and the gcd formula (uses recursion) to calculate the greatest common denominator of an array of numbers.

-
const arrayGcd = arr => {
-  const gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  return arr.reduce((a, b) => gcd(a, b));
-};
-
-
arrayGcd([1, 2, 3, 4, 5]); // 1
-arrayGcd([4, 8, 12]); // 4
-
-

arrayLcm

-

Calculates the lowest common multiple (lcm) of an array of numbers.

-

Use Array.reduce() and the lcm formula (uses recursion) to calculate the lowest common multiple of an array of numbers.

-
const arrayLcm = arr => {
-  const gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  const lcm = (x, y) => x * y / gcd(x, y);
-  return arr.reduce((a, b) => lcm(a, b));
-};
-
-
arrayLcm([1, 2, 3, 4, 5]); // 60
-arrayLcm([4, 8, 12]); // 24
-
-

arrayMax

+

arrayMax

Returns the maximum value in an array.

Use Math.max() combined with the spread operator (...) to get the maximum value in the array.

const arrayMax = arr => Math.max(...arr);
@@ -1170,11 +1145,15 @@ Uses a mathematical formula to calculate the length of the array required.

fibonacciCountUntilNum(10); // 7
 

gcd

-

Calculates the greatest common divisor between two numbers.

-

Use recursion. +

Calculates the greatest common divisor between two or more numbers/arrays.

+

The helperGcdfunction uses recursion. Base case is when y equals 0. In this case, return x. Otherwise, return the GCD of y and the remainder of the division x/y.

-
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
+
const gcd = (...arr) => {
+  let data = [].concat(...arr);
+  const helperGcd = (x, y) => (!y ? x : gcd(y, x % y));
+  return data.reduce((a, b) => helperGcd(a, b));
+};
 
gcd(8, 36); // 4
 
@@ -1241,15 +1220,18 @@ Return false if any of them divides the given number, else return < isPrime(12); // false

lcm

-

Returns the least common multiple of two numbers.

+

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 = (x, y) => {
+
const lcm = (...arr) => {
+  let data = [].concat(...arr);
   const gcd = (x, y) => (!y ? x : gcd(y, x % y));
-  return Math.abs(x * y) / gcd(x, y);
+  const helperLcm = (x, y) => x * y / gcd(x, y);
+  return arr.reduce((a, b) => helperLcm(a, b));
 };
 
lcm(12, 7); // 84
+lcm([1, 3, 4], 5); // 60
 

median

Returns the median of an array of numbers.

diff --git a/snippets/gcd.md b/snippets/gcd.md index 8c4011b4d..f450bbfa1 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -11,7 +11,7 @@ const gcd = (...arr) => { let data = [].concat(...arr); const helperGcd = (x, y) => (!y ? x : gcd(y, x % y)); return data.reduce((a, b) => helperGcd(a, b)); -} +}; ``` ```js diff --git a/snippets/lcm.md b/snippets/lcm.md index 215158c30..e7bb6a3e0 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -10,11 +10,11 @@ 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)) -} + return arr.reduce((a, b) => helperLcm(a, b)); +}; ``` ```js lcm(12, 7); // 84 -lcm([1,3,4],5); // 60 +lcm([1, 3, 4], 5); // 60 ``` diff --git a/tag_database b/tag_database index 5f4e45246..8a579166f 100644 --- a/tag_database +++ b/tag_database @@ -1,7 +1,5 @@ anagrams:string arrayAverage:math -arrayGcd:array -arrayLcm:array arrayMax:array arrayMin:array arraySum:math