diff --git a/snippets/anagrams.md b/snippets/anagrams.md index b89194564..2238c4a5f 100644 --- a/snippets/anagrams.md +++ b/snippets/anagrams.md @@ -13,5 +13,8 @@ const anagrams = str => { return str.split('').reduce((acc, letter, i) => acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)), []); }; -// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba'] +``` + +```js +anagrams('abc') -> ['abc','acb','bac','bca','cab','cba'] ``` diff --git a/snippets/arrayAverage.md b/snippets/arrayAverage.md index 74c8cef97..92c54518b 100644 --- a/snippets/arrayAverage.md +++ b/snippets/arrayAverage.md @@ -6,5 +6,8 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val ```js const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length; -// arrayAverage([1,2,3]) -> 2 +``` + +```js +arrayAverage([1,2,3]) -> 2 ``` diff --git a/snippets/arrayGcd.md b/snippets/arrayGcd.md index 39a154059..df657f31a 100644 --- a/snippets/arrayGcd.md +++ b/snippets/arrayGcd.md @@ -9,6 +9,9 @@ const arrayGcd = arr =>{ const gcd = (x, y) => !y ? x : gcd(y, x % y); return arr.reduce((a,b) => gcd(a,b)); } -// arrayGcd([1,2,3,4,5]) -> 1 -// arrayGcd([4,8,12]) -> 4 +``` + +```js +arrayGcd([1,2,3,4,5]) -> 1 +arrayGcd([4,8,12]) -> 4 ``` diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md index 8d4e593fb..fd82831f5 100644 --- a/snippets/arrayLcm.md +++ b/snippets/arrayLcm.md @@ -7,9 +7,12 @@ Use `Array.reduce()` and the `lcm` formula (uses recursion) to calculate the low ```js const arrayLcm = arr =>{ const gcd = (x, y) => !y ? x : gcd(y, x % y); - const lcm = (x, y) => (x*y)/gcd(x, y); + const lcm = (x, y) => (x*y)/gcd(x, y); return arr.reduce((a,b) => lcm(a,b)); } -// arrayLcm([1,2,3,4,5]) -> 60 -// arrayLcm([4,8,12]) -> 24 +``` + +```js +arrayLcm([1,2,3,4,5]) -> 60 +arrayLcm([4,8,12]) -> 24 ``` diff --git a/snippets/arrayMax.md b/snippets/arrayMax.md index 0c0022996..3e1b5c5e8 100644 --- a/snippets/arrayMax.md +++ b/snippets/arrayMax.md @@ -6,5 +6,8 @@ Use `Math.max()` combined with the spread operator (`...`) to get the maximum va ```js const arrayMax = arr => Math.max(...arr); -// arrayMax([10, 1, 5]) -> 10 +``` + +```js +arrayMax([10, 1, 5]) -> 10 ``` diff --git a/snippets/arrayMin.md b/snippets/arrayMin.md index 5bdd95559..171d60977 100644 --- a/snippets/arrayMin.md +++ b/snippets/arrayMin.md @@ -6,5 +6,8 @@ Use `Math.min()` combined with the spread operator (`...`) to get the minimum va ```js const arrayMin = arr => Math.min(...arr); -// arrayMin([10, 1, 5]) -> 1 +``` + +```js +arrayMin([10, 1, 5]) -> 1 ``` diff --git a/snippets/arraySum.md b/snippets/arraySum.md index 265c7fc60..9c3178d35 100644 --- a/snippets/arraySum.md +++ b/snippets/arraySum.md @@ -6,5 +6,8 @@ Use `Array.reduce()` to add each value to an accumulator, initialized with a val ```js const arraySum = arr => arr.reduce((acc, val) => acc + val, 0); -// arraySum([1,2,3,4]) -> 10 +``` + +```js +arraySum([1,2,3,4]) -> 10 ``` diff --git a/snippets/arrayToHtmlList.md b/snippets/arrayToHtmlList.md index bf5ba5721..27a66369f 100644 --- a/snippets/arrayToHtmlList.md +++ b/snippets/arrayToHtmlList.md @@ -6,5 +6,8 @@ Use `Array.map()` and `document.querySelector()` to create a list of html tags. ```js const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`
  • ${item}
  • `); -// arrayToHtmlList(['item 1', 'item 2'],'myListID') +``` + +```js +arrayToHtmlList(['item 1', 'item 2'],'myListID') ``` diff --git a/snippets/bottomVisible.md b/snippets/bottomVisible.md index adac44881..485f1879f 100644 --- a/snippets/bottomVisible.md +++ b/snippets/bottomVisible.md @@ -7,5 +7,8 @@ Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of t ```js const bottomVisible = () => document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight); +``` + +```js // bottomVisible() -> true ``` diff --git a/snippets/call.md b/snippets/call.md index a7aec4808..e30b395e0 100644 --- a/snippets/call.md +++ b/snippets/call.md @@ -6,9 +6,10 @@ Use a closure to call a stored key with stored arguments. ```js const call = ( key, ...args ) => context => context[ key ]( ...args ); -/* +``` + +```js Promise.resolve( [ 1, 2, 3 ] ).then( call('map', x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ] const map = call.bind(null, 'map') Promise.resolve( [ 1, 2, 3 ] ).then( map( x => 2 * x ) ).then( console.log ) //[ 2, 4, 6 ] -*/ -``` +``` diff --git a/snippets/capitalize.md b/snippets/capitalize.md index d085a3034..435b69202 100644 --- a/snippets/capitalize.md +++ b/snippets/capitalize.md @@ -8,6 +8,9 @@ Omit the `lowerRest` parameter to keep the rest of the string intact, or set it ```js const capitalize = ([first,...rest], lowerRest = false) => first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join('')); -// capitalize('myName') -> 'MyName' -// capitalize('myName', true) -> 'Myname' +``` + +```js +capitalize('fooBar') -> 'FooBar' +capitalize('fooBar', true) -> 'Foobar' ``` diff --git a/snippets/capitalizeEveryWord.md b/snippets/capitalizeEveryWord.md index bfaaf00b6..b607f8f58 100644 --- a/snippets/capitalizeEveryWord.md +++ b/snippets/capitalizeEveryWord.md @@ -6,5 +6,8 @@ Use `replace()` to match the first character of each word and `toUpperCase()` to ```js const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase()); -// capitalizeEveryWord('hello world!') -> 'Hello World!' +``` + +```js +capitalizeEveryWord('hello world!') -> 'Hello World!' ``` diff --git a/snippets/chainAsync.md b/snippets/chainAsync.md index 66fb8a6ab..1fb4067c8 100644 --- a/snippets/chainAsync.md +++ b/snippets/chainAsync.md @@ -6,11 +6,12 @@ Loop through an array of functions containing asynchronous events, calling `next ```js const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); }; -/* +``` + +```js chainAsync([ next => { console.log('0 seconds'); setTimeout(next, 1000); }, next => { console.log('1 second'); setTimeout(next, 1000); }, next => { console.log('2 seconds'); } ]) -*/ ``` diff --git a/snippets/chunk.md b/snippets/chunk.md index 8ad2c3ced..122712351 100644 --- a/snippets/chunk.md +++ b/snippets/chunk.md @@ -9,5 +9,8 @@ If the original array can't be split evenly, the final chunk will contain the re ```js const chunk = (arr, size) => Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size)); -// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],[5]] +``` + +```js +chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],[5]] ``` diff --git a/snippets/clampNumber.md b/snippets/clampNumber.md index d1bf9d64f..907b54729 100644 --- a/snippets/clampNumber.md +++ b/snippets/clampNumber.md @@ -2,16 +2,19 @@ Clamps `num` within the inclusive `lower` and `upper` bounds. -If `lower` is greater than `upper`, swap them. -If `num` falls within the range, return `num`. +If `lower` is greater than `upper`, swap them. +If `num` falls within the range, return `num`. Otherwise, return the nearest number in the range. ```js const clampNumber = (num, lower, upper) => { if(lower > upper) upper = [lower, lower = upper][0]; - return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper) + return (num>=lower && num<=upper) ? num : ((num < lower) ? lower : upper) } -// clampNumber(2, 3, 5) -> 3 -// clampNumber(1, -1, -5) -> -1 -// clampNumber(3, 2, 4) -> 3 +``` + +```js +clampNumber(2, 3, 5) -> 3 +clampNumber(1, -1, -5) -> -1 +clampNumber(3, 2, 4) -> 3 ```