From df4d48b80c4c4192e90a8db1440fffadd6c387f3 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 22 Dec 2017 15:00:46 +0330 Subject: [PATCH 1/3] Remove redundant number characters check Remove unnecessary multiple array making with `.split()` Reducing lines and making it more terse and readable --- snippets/toDecimalMark.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/snippets/toDecimalMark.md b/snippets/toDecimalMark.md index 79cea1840..deb196bed 100644 --- a/snippets/toDecimalMark.md +++ b/snippets/toDecimalMark.md @@ -6,12 +6,8 @@ Use `toString()` to convert the float `num` to a string, then use regex to separ ```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; + let numberParts = num.toString().split('.') + return `${numberParts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')}.${numberParts[1]}` } // toDecimalMark(12305030388.9087) //-> '12,305,030,388.9087' -// toDecimalMark(123.889087e2) //-> '12,388.9087' -// toDecimalMark('12305abc030388.9087') // -> '12,305,030,388.9087' ``` From 0bce00743e52f3949eabd74eb4177e4a1386da54 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 22 Dec 2017 15:03:55 +0330 Subject: [PATCH 2/3] Update description adding template literal explanation --- snippets/toDecimalMark.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snippets/toDecimalMark.md b/snippets/toDecimalMark.md index deb196bed..0a92fc73c 100644 --- a/snippets/toDecimalMark.md +++ b/snippets/toDecimalMark.md @@ -4,6 +4,8 @@ Convert a float-point arithmetic to the [Decimal mark](https://en.wikipedia.org/ 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. +Use [Ttemplate Literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to return joined parts. + ```js const toDecimalMark = (num) => { let numberParts = num.toString().split('.') From c3db1940f703d1025feee5f5c32400fc6ab49491 Mon Sep 17 00:00:00 2001 From: Soorena Date: Fri, 22 Dec 2017 18:34:37 +0330 Subject: [PATCH 3/3] Update toDecimalMark.md --- snippets/toDecimalMark.md | 1 - 1 file changed, 1 deletion(-) diff --git a/snippets/toDecimalMark.md b/snippets/toDecimalMark.md index 0a92fc73c..802578ec5 100644 --- a/snippets/toDecimalMark.md +++ b/snippets/toDecimalMark.md @@ -3,7 +3,6 @@ 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. - Use [Ttemplate Literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) to return joined parts. ```js