From c244042665a33e75f0f07268e669c766320bfae0 Mon Sep 17 00:00:00 2001 From: Rohit Date: Thu, 21 Dec 2017 20:35:45 +0530 Subject: [PATCH 1/7] Add snippet arrayLcm --- snippets/arrayLcm.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/arrayLcm.md diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md new file mode 100644 index 000000000..60a2c94d8 --- /dev/null +++ b/snippets/arrayLcm.md @@ -0,0 +1,15 @@ +### 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)); +} +// arrayLcm([1,2,3,4,5]) -> 60 +// arrayLcm([4,8,12]) -> 12 +``` \ No newline at end of file From a6e9d8907ac15beb4f87d8829cbafb14ebfb666d Mon Sep 17 00:00:00 2001 From: David Wu Date: Thu, 21 Dec 2017 16:22:00 +0100 Subject: [PATCH 2/7] Update push.sh --- .travis/push.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis/push.sh b/.travis/push.sh index 2f7ce8d71..277140235 100755 --- a/.travis/push.sh +++ b/.travis/push.sh @@ -12,6 +12,7 @@ commit_website_files() { } upload_files() { + echo "https://${GH_TOKEN}@github.com/Chalarangelo/30-seconds-of-code.git" git push --force "https://${GH_TOKEN}@github.com/Chalarangelo/30-seconds-of-code.git" master > /dev/null 2>&1 } From 16cba8a454c2262953734d7637dac30057dbee7c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Dec 2017 17:27:07 +0200 Subject: [PATCH 3/7] Update arrayLcm.md Fixed second example as @iamsoorena suggested --- snippets/arrayLcm.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md index 60a2c94d8..5439d5924 100644 --- a/snippets/arrayLcm.md +++ b/snippets/arrayLcm.md @@ -11,5 +11,5 @@ const arrayLcm = arr =>{ return arr.reduce((a,b) => lcm(a,b)); } // arrayLcm([1,2,3,4,5]) -> 60 -// arrayLcm([4,8,12]) -> 12 -``` \ No newline at end of file +// arrayLcm([4,8,12]) -> 24 +``` From 944ccac77eb290db9cef0f27c6e231dd77edc8f0 Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Thu, 21 Dec 2017 15:39:03 +0000 Subject: [PATCH 4/7] Travis build: 43 --- README.md | 21 +++++++++++++++++++++ docs/index.html | 14 ++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/README.md b/README.md index 96dfe3c60..bfb0a410b 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ * [`isSymbol`](#issymbol) * [`RGBToHex`](#rgbtohex) * [`timeTaken`](#timetaken) +* [`toDecimalMark`](#todecimalmark) * [`toOrdinalSuffix`](#toordinalsuffix) * [`UUIDGenerator`](#uuidgenerator) * [`validateEmail`](#validateemail) @@ -2091,6 +2092,26 @@ const timeTaken = callback => { [⬆ back to top](#table-of-contents) +### toDecimalMark + +Convert a float-point arithmetic to the [Decimal mark](https://en.wikipedia.org/wiki/Decimal_mark) form. + +Use `toString()` to convert the float `num` to a string, then use regex to separate every three characters of the integer part with a comma. + + ```js +const toDecimalMark = (num) => { + let cleanNum = num.toString().split('').filter(n => '0123456789.'.includes(n)).join('') + let wholeNum = cleanNum.split('.')[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + let decNum = `.${cleanNum.split('.')[1]}` + return wholeNum + decNum; +} +// toDecimalMark(12305030388.9087) //-> '12,305,030,388.9087' +// toDecimalMark(123.889087e2) //-> '12,388.9087' +// toDecimalMark('12305abc030388.9087') // -> '12,305,030,388.9087' +``` + +[⬆ back to top](#table-of-contents) + ### toOrdinalSuffix Adds an ordinal suffix to a number. diff --git a/docs/index.html b/docs/index.html index 0423fcd3e..deec6540b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -181,6 +181,7 @@ isSymbol RGBToHex timeTaken +toDecimalMark toOrdinalSuffix UUIDGenerator validateEmail @@ -1270,6 +1271,19 @@ Omit the second argument to use the default regex.

// timeTaken(() => Math.pow(2, 10)) -> 1024 // (logged): timeTaken: 0.02099609375ms +

toDecimalMark

+

Convert a float-point arithmetic to the Decimal mark form.

+

Use toString() to convert the float num to a string, then use regex to separate every three characters of the integer part with a comma.

+
const toDecimalMark = (num) => {
+ let cleanNum = num.toString().split('').filter(n => '0123456789.'.includes(n)).join('') 
+ let wholeNum = cleanNum.split('.')[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",")
+ let decNum = `.${cleanNum.split('.')[1]}`
+ return wholeNum + decNum;
+}
+// toDecimalMark(12305030388.9087) //-> '12,305,030,388.9087'
+// toDecimalMark(123.889087e2) //-> '12,388.9087'
+// toDecimalMark('12305abc030388.9087') // -> '12,305,030,388.9087'
+

toOrdinalSuffix

Adds an ordinal suffix to a number.

Use the modulo operator (%) to find values of single and tens digits. From e4ca5d0ab7e7bc4db892a8b796a6e178019825d6 Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Thu, 21 Dec 2017 15:44:47 +0000 Subject: [PATCH 5/7] Travis build: 45 --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 2571d8868..1012e4456 100644 --- a/tag_database +++ b/tag_database @@ -1,6 +1,7 @@ anagrams:string arrayAverage:math arrayGcd:array +arrayLcm: arrayMax:array arrayMin:array arraySum:math From 2357c5950a7076ba40fcc15ec5de967a77762f04 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 21 Dec 2017 17:45:06 +0200 Subject: [PATCH 6/7] Tag --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 2571d8868..6e965bf45 100644 --- a/tag_database +++ b/tag_database @@ -1,6 +1,7 @@ anagrams:string arrayAverage:math arrayGcd:array +arrayLcm:array arrayMax:array arrayMin:array arraySum:math From 90243d686ed1341aa9384cd8a4cb6f269c17a744 Mon Sep 17 00:00:00 2001 From: Pl4gue Date: Thu, 21 Dec 2017 15:51:36 +0000 Subject: [PATCH 7/7] Travis build: 47 --- README.md | 19 +++++++++++++++++++ docs/index.html | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/README.md b/README.md index bfb0a410b..abb50682e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ ### Array * [`arrayGcd`](#arraygcd) +* [`arrayLcm`](#arraylcm) * [`arrayMax`](#arraymax) * [`arrayMin`](#arraymin) * [`chunk`](#chunk) @@ -175,6 +176,24 @@ const arrayGcd = arr =>{ [⬆ 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)); +} +// 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. diff --git a/docs/index.html b/docs/index.html index deec6540b..ef88e9600 100644 --- a/docs/index.html +++ b/docs/index.html @@ -43,6 +43,7 @@

Array

arrayGcd +arrayLcm arrayMax arrayMin chunk @@ -198,6 +199,17 @@ // 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.