Merge branch 'master' of https://github.com/Chalarangelo/30-seconds-of-code
This commit is contained in:
@ -12,6 +12,7 @@ commit_website_files() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
upload_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
|
git push --force "https://${GH_TOKEN}@github.com/Chalarangelo/30-seconds-of-code.git" master > /dev/null 2>&1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
40
README.md
40
README.md
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
### Array
|
### Array
|
||||||
* [`arrayGcd`](#arraygcd)
|
* [`arrayGcd`](#arraygcd)
|
||||||
|
* [`arrayLcm`](#arraylcm)
|
||||||
* [`arrayMax`](#arraymax)
|
* [`arrayMax`](#arraymax)
|
||||||
* [`arrayMin`](#arraymin)
|
* [`arrayMin`](#arraymin)
|
||||||
* [`chunk`](#chunk)
|
* [`chunk`](#chunk)
|
||||||
@ -150,6 +151,7 @@
|
|||||||
* [`isSymbol`](#issymbol)
|
* [`isSymbol`](#issymbol)
|
||||||
* [`RGBToHex`](#rgbtohex)
|
* [`RGBToHex`](#rgbtohex)
|
||||||
* [`timeTaken`](#timetaken)
|
* [`timeTaken`](#timetaken)
|
||||||
|
* [`toDecimalMark`](#todecimalmark)
|
||||||
* [`toOrdinalSuffix`](#toordinalsuffix)
|
* [`toOrdinalSuffix`](#toordinalsuffix)
|
||||||
* [`UUIDGenerator`](#uuidgenerator)
|
* [`UUIDGenerator`](#uuidgenerator)
|
||||||
* [`validateEmail`](#validateemail)
|
* [`validateEmail`](#validateemail)
|
||||||
@ -174,6 +176,24 @@ const arrayGcd = arr =>{
|
|||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ 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
|
### arrayMax
|
||||||
|
|
||||||
Returns the maximum value in an array.
|
Returns the maximum value in an array.
|
||||||
@ -2091,6 +2111,26 @@ const timeTaken = callback => {
|
|||||||
|
|
||||||
[⬆ back to top](#table-of-contents)
|
[⬆ 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
|
### toOrdinalSuffix
|
||||||
|
|
||||||
Adds an ordinal suffix to a number.
|
Adds an ordinal suffix to a number.
|
||||||
|
|||||||
@ -43,6 +43,7 @@
|
|||||||
|
|
||||||
<h3>Array
|
<h3>Array
|
||||||
</h3><a class="sublink-1" href="#arraygcd">arrayGcd</a>
|
</h3><a class="sublink-1" href="#arraygcd">arrayGcd</a>
|
||||||
|
<a class="sublink-1" href="#arraylcm">arrayLcm</a>
|
||||||
<a class="sublink-1" href="#arraymax">arrayMax</a>
|
<a class="sublink-1" href="#arraymax">arrayMax</a>
|
||||||
<a class="sublink-1" href="#arraymin">arrayMin</a>
|
<a class="sublink-1" href="#arraymin">arrayMin</a>
|
||||||
<a class="sublink-1" href="#chunk">chunk</a>
|
<a class="sublink-1" href="#chunk">chunk</a>
|
||||||
@ -181,6 +182,7 @@
|
|||||||
<a class="sublink-1" href="#issymbol">isSymbol</a>
|
<a class="sublink-1" href="#issymbol">isSymbol</a>
|
||||||
<a class="sublink-1" href="#rgbtohex">RGBToHex</a>
|
<a class="sublink-1" href="#rgbtohex">RGBToHex</a>
|
||||||
<a class="sublink-1" href="#timetaken">timeTaken</a>
|
<a class="sublink-1" href="#timetaken">timeTaken</a>
|
||||||
|
<a class="sublink-1" href="#todecimalmark">toDecimalMark</a>
|
||||||
<a class="sublink-1" href="#toordinalsuffix">toOrdinalSuffix</a>
|
<a class="sublink-1" href="#toordinalsuffix">toOrdinalSuffix</a>
|
||||||
<a class="sublink-1" href="#uuidgenerator">UUIDGenerator</a>
|
<a class="sublink-1" href="#uuidgenerator">UUIDGenerator</a>
|
||||||
<a class="sublink-1" href="#validateemail">validateEmail</a>
|
<a class="sublink-1" href="#validateemail">validateEmail</a>
|
||||||
@ -197,6 +199,17 @@
|
|||||||
// arrayGcd([1,2,3,4,5]) -> 1
|
// arrayGcd([1,2,3,4,5]) -> 1
|
||||||
// arrayGcd([4,8,12]) -> 4
|
// arrayGcd([4,8,12]) -> 4
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraylcm">arrayLcm</h3></div><div class="section double-padded">
|
||||||
|
<p>Calculates the lowest common multiple (lcm) of an array of numbers.</p>
|
||||||
|
<p>Use <code>Array.reduce()</code> and the <code>lcm</code> formula (uses recursion) to calculate the lowest common multiple of an array of numbers.</p>
|
||||||
|
<pre><code class="language-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
|
||||||
|
</code></pre>
|
||||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraymax">arrayMax</h3></div><div class="section double-padded">
|
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="arraymax">arrayMax</h3></div><div class="section double-padded">
|
||||||
<p>Returns the maximum value in an array.</p>
|
<p>Returns the maximum value in an array.</p>
|
||||||
<p>Use <code>Math.max()</code> combined with the spread operator (<code>...</code>) to get the maximum value in the array.</p>
|
<p>Use <code>Math.max()</code> combined with the spread operator (<code>...</code>) to get the maximum value in the array.</p>
|
||||||
@ -1270,6 +1283,19 @@ Omit the second argument to use the default regex.</p>
|
|||||||
// timeTaken(() => Math.pow(2, 10)) -> 1024
|
// timeTaken(() => Math.pow(2, 10)) -> 1024
|
||||||
// (logged): timeTaken: 0.02099609375ms
|
// (logged): timeTaken: 0.02099609375ms
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="todecimalmark">toDecimalMark</h3></div><div class="section double-padded">
|
||||||
|
<p>Convert a float-point arithmetic to the <a href="https://en.wikipedia.org/wiki/Decimal_mark">Decimal mark</a> form.</p>
|
||||||
|
<p>Use <code>toString()</code> to convert the float <code>num</code> to a string, then use regex to separate every three characters of the integer part with a comma.</p>
|
||||||
|
<pre><code class="language-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'
|
||||||
|
</code></pre>
|
||||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="toordinalsuffix">toOrdinalSuffix</h3></div><div class="section double-padded">
|
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="toordinalsuffix">toOrdinalSuffix</h3></div><div class="section double-padded">
|
||||||
<p>Adds an ordinal suffix to a number.</p>
|
<p>Adds an ordinal suffix to a number.</p>
|
||||||
<p>Use the modulo operator (<code>%</code>) to find values of single and tens digits.
|
<p>Use the modulo operator (<code>%</code>) to find values of single and tens digits.
|
||||||
|
|||||||
15
snippets/arrayLcm.md
Normal file
15
snippets/arrayLcm.md
Normal file
@ -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]) -> 24
|
||||||
|
```
|
||||||
@ -1,6 +1,7 @@
|
|||||||
anagrams:string
|
anagrams:string
|
||||||
arrayAverage:math
|
arrayAverage:math
|
||||||
arrayGcd:array
|
arrayGcd:array
|
||||||
|
arrayLcm:array
|
||||||
arrayMax:array
|
arrayMax:array
|
||||||
arrayMin:array
|
arrayMin:array
|
||||||
arraySum:math
|
arraySum:math
|
||||||
|
|||||||
Reference in New Issue
Block a user