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 260d7b634..bd7472567 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)) - arr.length = 0; + 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 52e1eac21..97a59a0ed 100644 --- a/snippets/pullAtValue.md +++ b/snippets/pullAtValue.md @@ -4,21 +4,22 @@ 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) => { - let removed = [], + let removed = [], pushToRemove = arr.forEach((v, i) => pullArr.includes(v) ? removed.push(v) : v), mutateTo = arr.filter((v, i) => !pullArr.includes(v)); 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' ] -*/ ``` diff --git a/snippets/randomHexColorCode.md b/snippets/randomHexColorCode.md index 83d7d0f91..a40f129e2 100644 --- a/snippets/randomHexColorCode.md +++ b/snippets/randomHexColorCode.md @@ -2,11 +2,14 @@ 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 randomHexColorCode = () => '#'+(Math.random()*0xFFFFFF<<0).toString(16); -// 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 6433c6059..1939a78cd 100644 --- a/snippets/readFileLines.md +++ b/snippets/readFileLines.md @@ -9,13 +9,14 @@ 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('\n'); -/* - contents of test.txt : - line1 - line2 - line3 - ___________________________ - let arr = readFileLines('test.txt') - console.log(arr) // -> ['line1', 'line2', 'line3'] - */ +``` + +```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 34b10aa24..78be3257b 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 888965713..2d892b506 100644 --- a/snippets/repeatString.md +++ b/snippets/repeatString.md @@ -8,6 +8,9 @@ If no string is provided the default is `""` and the default number of times is 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' ``` 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' ```