From 39164e9cbf68fc32775afa9c6c3f40f318f62cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 25 Dec 2017 14:38:49 +0100 Subject: [PATCH] update snippets 96-107 --- snippets/RGBToHex.md | 5 ++++- snippets/pullAtIndex.md | 20 +++++++++++--------- snippets/pullAtValue.md | 13 +++++++------ snippets/randomHexColor.md | 11 +++++++---- snippets/randomIntegerInRange.md | 5 ++++- snippets/randomNumberInRange.md | 5 ++++- snippets/readFileLines.md | 23 +++++++++++++---------- snippets/redirect.md | 5 ++++- snippets/remove.md | 5 ++++- snippets/repeatString.md | 13 ++++++++----- snippets/reverseString.md | 5 ++++- 11 files changed, 70 insertions(+), 40 deletions(-) diff --git a/snippets/RGBToHex.md b/snippets/RGBToHex.md index eb13cbeb4..f7568f70f 100644 --- a/snippets/RGBToHex.md +++ b/snippets/RGBToHex.md @@ -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' ``` diff --git a/snippets/pullAtIndex.md b/snippets/pullAtIndex.md index e848cc7c7..459c2c7b2 100644 --- a/snippets/pullAtIndex.md +++ b/snippets/pullAtIndex.md @@ -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.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 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' ] ``` diff --git a/snippets/pullAtValue.md b/snippets/pullAtValue.md index c092933e4..ff2559a53 100644 --- a/snippets/pullAtValue.md +++ b/snippets/pullAtValue.md @@ -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.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 const pullAtValue = (arr, pullArr) => { @@ -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' ] ``` diff --git a/snippets/randomHexColor.md b/snippets/randomHexColor.md index f7cdbc26e..b511c10d7 100644 --- a/snippets/randomHexColor.md +++ b/snippets/randomHexColor.md @@ -2,7 +2,7 @@ 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 const randomHexColor = () => { @@ -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" ``` diff --git a/snippets/randomIntegerInRange.md b/snippets/randomIntegerInRange.md index 2d35fd279..e8fd26077 100644 --- a/snippets/randomIntegerInRange.md +++ b/snippets/randomIntegerInRange.md @@ -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 ``` diff --git a/snippets/randomNumberInRange.md b/snippets/randomNumberInRange.md index a92dfff54..53d60b16d 100644 --- a/snippets/randomNumberInRange.md +++ b/snippets/randomNumberInRange.md @@ -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 ``` diff --git a/snippets/readFileLines.md b/snippets/readFileLines.md index 17cb4788f..2492195c0 100644 --- a/snippets/readFileLines.md +++ b/snippets/readFileLines.md @@ -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/); -/* - contents of test.txt : - line1 - line2 - line3 - ___________________________ - let arr = readFileLines('test.txt') - console.log(arr) // -> ['line1', 'line2', 'line3'] - */ +const readFileLines = filename => fs.readFileSync(filename).toString('UTF8').split('\n'); +``` + +```js +/* +contents of test.txt : + line1 + line2 + line3 + ___________________________ +*/ +let arr = readFileLines('test.txt') +console.log(arr) // ['line1', 'line2', 'line3'] ``` diff --git a/snippets/redirect.md b/snippets/redirect.md index d98e2caf2..006d5acd0 100644 --- a/snippets/redirect.md +++ b/snippets/redirect.md @@ -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') ``` diff --git a/snippets/remove.md b/snippets/remove.md index 25606c21b..cfbcba598 100644 --- a/snippets/remove.md +++ b/snippets/remove.md @@ -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] ``` diff --git a/snippets/repeatString.md b/snippets/repeatString.md index c7478acb2..2f66bb047 100644 --- a/snippets/repeatString.md +++ b/snippets/repeatString.md @@ -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) => { - return num >= 0 ? str.repeat(num) : str; -}; -// repeatString("abc",3) -> 'abcabcabc' -// repeatString("abc") -> 'abcabc' +const repeatString = (str="",num=2) => { + return num >= 0 ? str.repeat(num) : str; +} +``` + +```js +repeatString("abc",3) // 'abcabcabc' +repeatString("abc") // 'abcabc' ``` diff --git a/snippets/reverseString.md b/snippets/reverseString.md index 5564a5897..2fe9cdd0b 100644 --- a/snippets/reverseString.md +++ b/snippets/reverseString.md @@ -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' ```