update snippets 96-107

This commit is contained in:
Stefan Feješ
2017-12-25 14:38:49 +01:00
parent bc58a5bf54
commit 5b23bade1c
11 changed files with 61 additions and 33 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

@ -15,10 +15,12 @@ const pullAtIndex = (arr, pullArr) => {
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

@ -15,10 +15,11 @@ const pullAtValue = (arr, pullArr) => {
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

@ -6,7 +6,10 @@ Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use b
```js ```js
const randomHexColorCode = () => '#'+(Math.random()*0xFFFFFF<<0).toString(16); const randomHexColorCode = () => '#'+(Math.random()*0xFFFFFF<<0).toString(16);
// 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

@ -9,7 +9,9 @@ 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('\n'); const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').split('\n');
/* ```
```js
contents of test.txt : contents of test.txt :
line1 line1
line2 line2
@ -17,5 +19,4 @@ const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').spl
___________________________ ___________________________
let arr = readFileLines('test.txt') let arr = readFileLines('test.txt')
console.log(arr) // -> ['line1', 'line2', 'line3'] 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

@ -8,6 +8,9 @@ If no string is provided the default is `""` and the default number of times is
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'
``` ```