diff --git a/snippets/cleanObj.md b/snippets/cleanObj.md index 57ec325e0..3f5207369 100644 --- a/snippets/cleanObj.md +++ b/snippets/cleanObj.md @@ -20,6 +20,5 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => { ```js const testObj = {a: 1, b: 2, children: {a: 1, b: 2}} -cleanObj(testObj, ["a"],"children") -console.log(testObj) // { a: 1, children : { a: 1}} +cleanObj(testObj, ["a"],"children") // { a: 1, children : { a: 1}} ``` diff --git a/snippets/digitize.md b/snippets/digitize.md index 6a48fafcb..9d52fd04b 100644 --- a/snippets/digitize.md +++ b/snippets/digitize.md @@ -6,6 +6,9 @@ Convert the number to a string, using spread operators in ES6(`[...string]`) bui Use `Array.map()` and `parseInt()` to transform each value to an integer. ```js -const digitize = n => [...'' + n].map(i => parseInt(i)); -// digitize(2334) -> [2, 3, 3, 4] +const digitize = n => [...''+n].map(i => parseInt(i)); +``` + +```js +differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a,b) => Math.round(a) == Math.round(b)) // [1, 1.2] ``` diff --git a/snippets/distance.md b/snippets/distance.md index 5595ef188..543c763a6 100644 --- a/snippets/distance.md +++ b/snippets/distance.md @@ -6,5 +6,8 @@ Use `Math.hypot()` to calculate the Euclidean distance between two points. ```js const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); -// distance(1,1, 2,3) -> 2.23606797749979 +``` + +```js +distance(1,1, 2,3) -> 2.23606797749979 ``` diff --git a/snippets/distinctValuesOfArray.md b/snippets/distinctValuesOfArray.md index 24c0c60c0..374e24cef 100644 --- a/snippets/distinctValuesOfArray.md +++ b/snippets/distinctValuesOfArray.md @@ -6,5 +6,8 @@ Use ES6 `Set` and the `...rest` operator to discard all duplicated values. ```js const distinctValuesOfArray = arr => [...new Set(arr)]; -// distinctValuesOfArray([1,2,2,3,4,4,5]) -> [1,2,3,4,5] +``` + +```js +distinctValuesOfArray([1,2,2,3,4,4,5]) -> [1,2,3,4,5] ``` diff --git a/snippets/dropElements.md b/snippets/dropElements.md index adc81d79d..064a16d22 100644 --- a/snippets/dropElements.md +++ b/snippets/dropElements.md @@ -10,5 +10,8 @@ const dropElements = (arr, func) => { while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; }; -// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] +``` + +```js +dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] ``` diff --git a/snippets/dropRight.md b/snippets/dropRight.md index 3e6716f0e..cdf8fd458 100644 --- a/snippets/dropRight.md +++ b/snippets/dropRight.md @@ -6,7 +6,10 @@ Use `Array.slice()` to slice the remove the specified number of elements from th ```js const dropRight = (arr, n = 1) => arr.slice(0, -n); -// dropRight([1,2,3]) -> [1,2] -// dropRight([1,2,3], 2) -> [1] -// dropRight([1,2,3], 42) -> [] +``` + +```js +dropRight([1,2,3]) // [1,2] +dropRight([1,2,3], 2) // [1] +dropRight([1,2,3], 42) // [] ``` diff --git a/snippets/elementIsVisibleInViewport.md b/snippets/elementIsVisibleInViewport.md index 01015d4fc..83e6afe71 100644 --- a/snippets/elementIsVisibleInViewport.md +++ b/snippets/elementIsVisibleInViewport.md @@ -16,7 +16,10 @@ const elementIsVisibleInViewport = (el, partiallyVisible = false) => { ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; }; -// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10} -// elementIsVisibleInViewport(el) -> false (not fully visible) -// elementIsVisibleInViewport(el, true) -> true (partially visible) +``` + +```js +// e.g. 100x100 viewport and a 10x10px element at position {top: -1, left: 0, bottom: 9, right: 10} +elementIsVisibleInViewport(el) -> false // (not fully visible) +elementIsVisibleInViewport(el, true) -> true // (partially visible) ``` diff --git a/snippets/escapeRegExp.md b/snippets/escapeRegExp.md index caed74069..7fdbff462 100644 --- a/snippets/escapeRegExp.md +++ b/snippets/escapeRegExp.md @@ -6,5 +6,8 @@ Use `replace()` to escape special characters. ```js const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); -// escapeRegExp('(test)') -> \\(test\\) +``` + +```js +escapeRegExp('(test)') -> \\(test\\) ``` diff --git a/snippets/everyNth.md b/snippets/everyNth.md index f326ebe2b..6aa78ac69 100644 --- a/snippets/everyNth.md +++ b/snippets/everyNth.md @@ -6,5 +6,8 @@ Use `Array.filter()` to create a new array that contains every nth element of a ```js const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1); -// everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ] +``` + +```js +everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ] ``` diff --git a/snippets/extendHex.md b/snippets/extendHex.md index a134ebd7b..40253a7f1 100644 --- a/snippets/extendHex.md +++ b/snippets/extendHex.md @@ -6,7 +6,10 @@ Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for con `String.slice()` is used to remove `#` from string start since it's added once. ```js const extendHex = shortHex => - '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x + x).join(''); -// extendHex('#03f') -> '#0033ff' -// extendHex('05a') -> '#0055aa' + '#' + shortHex.slice(shortHex.startsWith('#') ? 1 : 0).split('').map(x => x+x).join('') +``` + +```js +extendHex('#03f') // '#0033ff' +extendHex('05a') // '#0055aa' ``` diff --git a/snippets/factorial.md b/snippets/factorial.md index e0afbd2c3..de604f52c 100644 --- a/snippets/factorial.md +++ b/snippets/factorial.md @@ -11,5 +11,8 @@ Throws an exception if `n` is a negative number. const factorial = n => n < 0 ? (() => { throw new TypeError('Negative numbers are not allowed!'); })() : n <= 1 ? 1 : n * factorial(n - 1); -// factorial(6) -> 720 +``` + +```js +factorial(6) -> 720 ``` diff --git a/snippets/fibonacci.md b/snippets/fibonacci.md index e5e2e15fd..9addac34a 100644 --- a/snippets/fibonacci.md +++ b/snippets/fibonacci.md @@ -8,5 +8,8 @@ Use `Array.reduce()` to add values into the array, using the sum of the last two ```js const fibonacci = n => Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []); -// fibonacci(5) -> [0,1,1,2,3] +``` + +```js +factorial(6) -> 720 ``` diff --git a/snippets/fibonacciCountUntilNum.md b/snippets/fibonacciCountUntilNum.md index 8cf904ef0..82f53ea98 100644 --- a/snippets/fibonacciCountUntilNum.md +++ b/snippets/fibonacciCountUntilNum.md @@ -6,6 +6,9 @@ Use a mathematical formula to calculate the number of fibonacci numbers until `n ```js const fibonacciCountUntilNum = num => - Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); -// fibonacciCountUntilNum(10) -> 7 + Math.ceil(Math.log(num * Math.sqrt(5) + 1/2) / Math.log((Math.sqrt(5)+1)/2)); +``` + +```js +fibonacciCountUntilNum(10) -> 7 ``` diff --git a/snippets/fibonacciUntilNum.md b/snippets/fibonacciUntilNum.md index 5e1b2c9d7..aef99ca37 100644 --- a/snippets/fibonacciUntilNum.md +++ b/snippets/fibonacciUntilNum.md @@ -10,6 +10,9 @@ Uses a mathematical formula to calculate the length of the array required. const fibonacciUntilNum = num => { let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2)); return Array.from({ length: n}).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []); -}; -// fibonacciUntilNum(15) -> [0,1,1,2,3,5,8,13] +} +``` + +```js +fibonacciCountUntilNum(10) // 7 ``` diff --git a/snippets/filterNonUnique.md b/snippets/filterNonUnique.md index 059d79aa5..1568afbf8 100644 --- a/snippets/filterNonUnique.md +++ b/snippets/filterNonUnique.md @@ -6,5 +6,8 @@ Use `Array.filter()` for an array containing only the unique values. ```js const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i)); -// filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5] +``` + +```js +filterNonUnique([1,2,2,3,4,4,5]) -> [1,3,5] ``` diff --git a/snippets/flatten.md b/snippets/flatten.md index 0efd29071..29cb52ad1 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -5,6 +5,9 @@ Flattens an array. Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays. ```js -const flatten = arr => [ ].concat(...arr); -// flatten([1,[2],3,4]) -> [1,2,3,4] +const flatten = arr => [ ].concat( ...arr ); +``` + +```js +flatten([1,[2],3,4]) // [1,2,3,4] ```