diff --git a/README.md b/README.md index bde7d8eff..ab1608e3d 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,6 @@
View contents -* [`arrayGcd`](#arraygcd) -* [`arrayLcm`](#arraylcm) -* [`arrayMax`](#arraymax) -* [`arrayMin`](#arraymin) * [`chunk`](#chunk) * [`compact`](#compact) * [`countOccurrences`](#countoccurrences) @@ -144,8 +140,7 @@
View contents -* [`arrayAverage`](#arrayaverage) -* [`arraySum`](#arraysum) +* [`average`](#average) * [`clampNumber`](#clampnumber) * [`collatz`](#collatz) * [`digitize`](#digitize) @@ -162,7 +157,9 @@ * [`isEven`](#iseven) * [`isPrime`](#isprime) * [`lcm`](#lcm) +* [`max`](#max) * [`median`](#median) +* [`min`](#min) * [`palindrome`](#palindrome) * [`percentile`](#percentile) * [`powerset`](#powerset) @@ -171,6 +168,7 @@ * [`randomNumberInRange`](#randomnumberinrange) * [`round`](#round) * [`standardDeviation`](#standarddeviation) +* [`sum`](#sum)
@@ -427,107 +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. - -Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array. - -```js -const arrayMax = arr => Math.max(...arr); -``` - -
-Examples - -```js -arrayMax([10, 1, 5]); // 10 -``` - -
- - -[⬆ Back to top](#table-of-contents) - - -### arrayMin - -Returns the minimum value in an array. - -Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array. - -```js -const arrayMin = arr => Math.min(...arr); -``` - -
-Examples - -```js -arrayMin([10, 1, 5]); // 1 -``` - -
- - -[⬆ Back to top](#table-of-contents) - - ### chunk Chunks an array into smaller arrays of a specified size. @@ -2308,44 +2205,21 @@ negate(isOdd)(1); // false ## Math -### arrayAverage +### average -Returns the average of an array of numbers. +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 arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length; +const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) / arr.length; ```
Examples ```js -arrayAverage([1, 2, 3]); // 2 -``` - -
- - -[⬆ Back to top](#table-of-contents) - - -### arraySum - -Returns the sum of an array of numbers. - -Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. - -```js -const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); -``` - -
-Examples - -```js -arraySum([1, 2, 3, 4]); // 10 +average([1, 2, 3]); // 2 ```
@@ -2567,14 +2441,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 +2628,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 +2647,61 @@ const lcm = (x, y) => { ```js lcm(12, 7); // 84 +lcm([1, 3, 4], 5); // 60 +``` + +
+ + +[⬆ Back to top](#table-of-contents) + + +### max + +Returns the maximum value out of two or more numbers/arrays. + +Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array. + +```js + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +const max = (...arr) => Math.max(...[].concat(...arr); +``` + +
+Examples + +```js +max([10, 1, 5]); // 10 ```
@@ -2804,6 +2739,29 @@ median([0, 10, -2, 7]); // 3.5 [⬆ Back to top](#table-of-contents) +### min + +Returns the minimum value in an array. + +Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array. + +```js +const min = arr => Math.min(...[].concat(...arr)); +``` + +
+Examples + +```js +min([10, 1, 5]); // 1 +``` + +
+ + +[⬆ Back to top](#table-of-contents) + + ### palindrome Returns `true` if the given string is a palindrome, `false` otherwise. @@ -3014,6 +2972,29 @@ standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (popu
+[⬆ Back to top](#table-of-contents) + + +### sum + +Returns the sum of an of two or more numbers/arrays. + +Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. + +```js +const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0); +``` + +
+Examples + +```js +sum([1, 2, 3, 4]); // 10 +``` + +
+ + [⬆ Back to top](#table-of-contents) ## Media diff --git a/docs/index.html b/docs/index.html index 9fbd42386..4068be7a2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -107,11 +107,7 @@ spreadOver

Array -

arrayGcd -arrayLcm -arrayMax -arrayMin -chunk +chunk compact countOccurrences deepFlatten @@ -189,8 +185,7 @@ negate

Math -

arrayAverage -arraySum +average clampNumber collatz digitize @@ -207,7 +202,9 @@ isEven isPrime lcm +max median +min palindrome percentile powerset @@ -216,6 +213,7 @@ randomNumberInRange round standardDeviation +sum

Media

speechSynthesis @@ -342,44 +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

-

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);
-
-
arrayMax([10, 1, 5]); // 10
-
-

arrayMin

-

Returns the minimum value in an array.

-

Use Math.min() combined with the spread operator (...) to get the minimum value in the array.

-
const arrayMin = arr => Math.min(...arr);
-
-
arrayMin([10, 1, 5]); // 1
-
-

chunk

+

chunk

Chunks an array into smaller arrays of a specified size.

Use Array.from() to create a new array, that fits the number of chunks that will be produced. Use Array.slice() to map each element of the new array to a chunk the length of size. @@ -1072,19 +1033,12 @@ runPromisesInSeries([() => delay(1000), () => delay(2000)]); // //executes negate(isOdd)(1); // false


Math

-

arrayAverage

-

Returns the average of an array of numbers.

+

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 arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
+
const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) / arr.length;
 
-
arrayAverage([1, 2, 3]); // 2
-
-

arraySum

-

Returns the sum of an array of numbers.

-

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

-
const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
-
-
arraySum([1, 2, 3, 4]); // 10
+
average([1, 2, 3]); // 2
 

clampNumber

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

@@ -1170,11 +1124,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 +1199,56 @@ 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
+
+

max

+

Returns the maximum value out of two or more numbers/arrays.

+

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

+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+const max = (...arr) => Math.max(...[].concat(...arr);
+
+
max([10, 1, 5]); // 10
 

median

Returns the median of an array of numbers.

@@ -1264,6 +1263,13 @@ Return the number at the midpoint if length is odd, otherwise the a
median([5, 6, 50, 1, -5]); // 5
 median([0, 10, -2, 7]); // 3.5
 
+

min

+

Returns the minimum value in an array.

+

Use Math.min() combined with the spread operator (...) to get the minimum value in the array.

+
const min = arr => Math.min(...[].concat(...arr));
+
+
min([10, 1, 5]); // 1
+

palindrome

Returns true if the given string is a palindrome, false otherwise.

Convert string toLowerCase() and use replace() to remove non-alphanumeric characters from it. @@ -1349,6 +1355,13 @@ You can omit the second argument to get the sample standard deviation or set it

standardDeviation([10, 2, 38, 23, 38, 23, 21]); // 13.284434142114991 (sample)
 standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
 
+

sum

+

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

+

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

+
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
+
+
sum([1, 2, 3, 4]); // 10
+

Media

speechSynthesis

Performs speech synthesis (experimental).

diff --git a/scripts/build-script.js b/scripts/build-script.js index 891b51cce..61d1c693b 100644 --- a/scripts/build-script.js +++ b/scripts/build-script.js @@ -73,7 +73,7 @@ try { if(capitalize(tag, true)=='Uncategorized') { uncategorizedOutput +=`## _${capitalize(tag, true)}_\n`; for(let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) - uncategorizedOutput += `\n${snippets[taggedSnippet[0]+'.md']+'\n[⬆ back to top](#table-of-contents)\n'}`; + uncategorizedOutput += `\n${snippets[taggedSnippet[0]+'.md']+'\n
[⬆ back to top](#table-of-contents)\n\n'}`; } else { output +=`## ${capitalize(tag, true)}\n`; for(let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)){ 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/snippets/max.md b/snippets/max.md index 9f6bde286..a2188fd48 100644 --- a/snippets/max.md +++ b/snippets/max.md @@ -5,6 +5,37 @@ Returns the maximum value out of two or more numbers/arrays. Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array. ```js + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + const max = (...arr) => Math.max(...[].concat(...arr); ``` diff --git a/tag_database b/tag_database index 5f4e45246..f552d03b2 100644 --- a/tag_database +++ b/tag_database @@ -1,11 +1,6 @@ anagrams:string -arrayAverage:math -arrayGcd:array -arrayLcm:array -arrayMax:array -arrayMin:array -arraySum:math arrayToHtmlList:browser +average:math bottomVisible:browser call:adapter capitalize:string @@ -81,7 +76,9 @@ JSONToFile:node last:array lcm:math mapObject:array +max:math median:math +min:math negate:logic nthElement:array objectFromPairs:object @@ -123,6 +120,7 @@ sortCharactersInString:string speechSynthesis:media spreadOver:adapter standardDeviation:math +sum:math symmetricDifference:array tail:array take:array