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
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

@ -10,15 +10,17 @@ Use `Array.push()` to keep track of pulled values
const pullAtIndex = (arr, pullArr) => {
let removed = [];
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;
pulled.forEach(v => arr.push(v));
return removed;
};
// let myArray = ['a', 'b', 'c', 'd'];
// let pulled = pullAtIndex(myArray, [1, 3]);
// console.log(myArray); -> [ 'a', 'c' ]
// console.log(pulled); -> [ 'b', 'd' ]
}
```
```js
let myArray = ['a', 'b', 'c', 'd'];
let pulled = pullAtIndex(myArray, [1, 3]);
console.log(myArray); // [ 'a', 'c' ]
console.log(pulled); // [ 'b', 'd' ]
```

View File

@ -14,11 +14,12 @@ const pullAtValue = (arr, pullArr) => {
arr.length = 0;
mutateTo.forEach(v => arr.push(v));
return removed;
};
/*
}
```
```js
let myArray = ['a', 'b', 'c', 'd'];
let pulled = pullAtValue(myArray, ['b', 'd']);
console.log(myArray); -> [ 'a', 'c' ]
console.log(pulled); -> [ 'b', 'd' ]
*/
console.log(myArray); // [ 'a', 'c' ]
console.log(pulled); // [ 'b', 'd' ]
```

View File

@ -10,7 +10,10 @@ const randomHexColor = () => {
return '#' + (n.length !== 6
? (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
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
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
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');
```
```js
/*
contents of test.txt :
contents of test.txt :
line1
line2
line3
___________________________
let arr = readFileLines('test.txt')
console.log(arr) // -> ['line1', 'line2', '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
const redirect = (url, asLink = true) =>
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);
}, [])
: [];
// 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.
```js
const repeatString = (str = '', num = 2) => {
const repeatString = (str="",num=2) => {
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
const reverseString = str => str.split('').reverse().join('');
// reverseString('foobar') -> 'raboof'
```
```js
reverseString('foobar') -> 'raboof'
```