update snippets 96-107

This commit is contained in:
Stefan Feješ
2017-12-25 14:38:49 +01:00
committed by Agamemnon Zorbas
parent a7fe8cb9d6
commit 6bedb8fba4
11 changed files with 70 additions and 40 deletions

View File

@ -6,5 +6,8 @@ Convert given RGB parameters to hexadecimal string using bitwise left-shift oper
```js ```js
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0'); const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
// RGBToHex(255, 165, 1) -> 'ffa501' ```
```js
RGBToHex(255, 165, 1) -> 'ffa501'
``` ```

View File

@ -4,21 +4,23 @@ Mutates the original array to filter out the values at the specified indexes.
Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
Use `Array.push()` to keep track of pulled values Use `Array.push()` to keep track of pulled values
```js ```js
const pullAtIndex = (arr, pullArr) => { const pullAtIndex = (arr, pullArr) => {
let removed = []; let removed = [];
let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v) let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v)
.filter((v, i) => !pullArr.includes(i)); .filter((v, i) => !pullArr.includes(i))
arr.length = 0; arr.length = 0;
pulled.forEach(v => arr.push(v)); pulled.forEach(v => arr.push(v));
return removed; return removed;
}; }
```
// let myArray = ['a', 'b', 'c', 'd'];
// let pulled = pullAtIndex(myArray, [1, 3]); ```js
let myArray = ['a', 'b', 'c', 'd'];
// console.log(myArray); -> [ 'a', 'c' ] let pulled = pullAtIndex(myArray, [1, 3]);
// console.log(pulled); -> [ 'b', 'd' ]
console.log(myArray); // [ 'a', 'c' ]
console.log(pulled); // [ 'b', 'd' ]
``` ```

View File

@ -4,7 +4,7 @@ Mutates the original array to filter out the values specified. Returns the remov
Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. Use `Array.length = 0` to mutate the passed in an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
Use `Array.push()` to keep track of pulled values Use `Array.push()` to keep track of pulled values
```js ```js
const pullAtValue = (arr, pullArr) => { const pullAtValue = (arr, pullArr) => {
@ -14,11 +14,12 @@ const pullAtValue = (arr, pullArr) => {
arr.length = 0; arr.length = 0;
mutateTo.forEach(v => arr.push(v)); mutateTo.forEach(v => arr.push(v));
return removed; return removed;
}; }
/* ```
```js
let myArray = ['a', 'b', 'c', 'd']; let myArray = ['a', 'b', 'c', 'd'];
let pulled = pullAtValue(myArray, ['b', 'd']); let pulled = pullAtValue(myArray, ['b', 'd']);
console.log(myArray); -> [ 'a', 'c' ] console.log(myArray); // [ 'a', 'c' ]
console.log(pulled); -> [ 'b', 'd' ] console.log(pulled); // [ 'b', 'd' ]
*/
``` ```

View File

@ -2,7 +2,7 @@
Generates a random hexadecimal color code. Generates a random hexadecimal color code.
Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using `toString(16)`. Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using `toString(16)`.
```js ```js
const randomHexColor = () => { const randomHexColor = () => {
@ -10,7 +10,10 @@ const randomHexColor = () => {
return '#' + (n.length !== 6 return '#' + (n.length !== 6
? (Math.random() * 0xf | 0).toString(16) + n : n); ? (Math.random() * 0xf | 0).toString(16) + n : n);
}; };
// randomHexColorCode() -> "#e34155" ```
// randomHexColorCode() -> "#fd73a6"
// randomHexColorCode() -> "#4144c6" ```js
randomHexColorCode() // "#e34155"
randomHexColorCode() // "#fd73a6"
randomHexColorCode() // "#4144c6"
``` ```

View File

@ -6,5 +6,8 @@ Use `Math.random()` to generate a random number and map it to the desired range,
```js ```js
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
// randomIntegerInRange(0, 5) -> 2 ```
```js
randomIntegerInRange(0, 5) -> 2
``` ```

View File

@ -6,5 +6,8 @@ Use `Math.random()` to generate a random value, map it to the desired range usin
```js ```js
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min; const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
// randomNumberInRange(2,10) -> 6.0211363285087005 ```
```js
randomNumberInRange(2,10) -> 6.0211363285087005
``` ```

View File

@ -8,14 +8,17 @@ creating an array from contents of file by `split`ing file content line by line
```js ```js
const fs = require('fs'); const fs = require('fs');
const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').split(/\r?\n/); const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').split('\n');
/* ```
contents of test.txt :
line1 ```js
line2 /*
line3 contents of test.txt :
___________________________ line1
let arr = readFileLines('test.txt') line2
console.log(arr) // -> ['line1', 'line2', 'line3'] line3
*/ ___________________________
*/
let arr = readFileLines('test.txt')
console.log(arr) // ['line1', 'line2', 'line3']
``` ```

View File

@ -8,5 +8,8 @@ Pass a second argument to simulate a link click (`true` - default) or an HTTP re
```js ```js
const redirect = (url, asLink = true) => const redirect = (url, asLink = true) =>
asLink ? window.location.href = url : window.location.replace(url); asLink ? window.location.href = url : window.location.replace(url);
// redirect('https://google.com') ```
```js
redirect('https://google.com')
``` ```

View File

@ -11,5 +11,8 @@ const remove = (arr, func) =>
arr.splice(arr.indexOf(val), 1); return acc.concat(val); arr.splice(arr.indexOf(val), 1); return acc.concat(val);
}, []) }, [])
: []; : [];
// remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] ```
```js
remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4]
``` ```

View File

@ -5,9 +5,12 @@ Repeats a string n times using `String.repeat()`
If no string is provided the default is `""` and the default number of times is 2. If no string is provided the default is `""` and the default number of times is 2.
```js ```js
const repeatString = (str = '', num = 2) => { const repeatString = (str="",num=2) => {
return num >= 0 ? str.repeat(num) : str; return num >= 0 ? str.repeat(num) : str;
}; }
// repeatString("abc",3) -> 'abcabcabc' ```
// repeatString("abc") -> 'abcabc'
```js
repeatString("abc",3) // 'abcabcabc'
repeatString("abc") // 'abcabc'
``` ```

View File

@ -7,5 +7,8 @@ Combine characters to get a string using `join('')`.
```js ```js
const reverseString = str => str.split('').reverse().join(''); const reverseString = str => str.split('').reverse().join('');
// reverseString('foobar') -> 'raboof' ```
```js
reverseString('foobar') -> 'raboof'
``` ```