From b2cffa68588b52496113b44422a17f8725657d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 25 Dec 2017 13:59:23 +0100 Subject: [PATCH] update snippets 1-15 --- snippets/anagrams.md | 5 ++++- snippets/arrayAverage.md | 5 ++++- snippets/arrayGcd.md | 11 +++++++---- snippets/arrayLcm.md | 13 ++++++++----- snippets/arrayMax.md | 5 ++++- snippets/arrayMin.md | 5 ++++- snippets/arraySum.md | 5 ++++- snippets/arrayToHtmlList.md | 7 +++++-- snippets/bottomVisible.md | 3 +++ snippets/call.md | 9 +++++---- snippets/capitalize.md | 7 +++++-- snippets/capitalizeEveryWord.md | 5 ++++- snippets/chainAsync.md | 5 +++-- snippets/chunk.md | 5 ++++- snippets/clampNumber.md | 13 ++++++++----- 15 files changed, 72 insertions(+), 31 deletions(-) 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 fde889d05..364b18792 100644 --- a/snippets/arrayGcd.md +++ b/snippets/arrayGcd.md @@ -7,8 +7,11 @@ Use `Array.reduce()` and the `gcd` formula (uses recursion) to calculate the gre ```js 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 + return arr.reduce((a,b) => gcd(a,b)); +} +``` + +```js +arrayGcd([1,2,3,4,5]) -> 1 +arrayGcd([4,8,12]) -> 4 ``` diff --git a/snippets/arrayLcm.md b/snippets/arrayLcm.md index 42cb61906..f77797c4c 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); - return arr.reduce((a, b) => lcm(a, b)); -}; -// arrayLcm([1,2,3,4,5]) -> 60 -// arrayLcm([4,8,12]) -> 24 + const lcm = (x, y) => (x*y)/gcd(x, y); + return arr.reduce((a,b) => lcm(a,b)); +} +``` + +```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 6577ff663..27a66369f 100644 --- a/snippets/arrayToHtmlList.md +++ b/snippets/arrayToHtmlList.md @@ -5,6 +5,9 @@ Converts the given array elements into `
  • ` tags and appends them to the list 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') +const arrayToHtmlList = (arr, listID) => arr.map(item => document.querySelector("#"+listID).innerHTML+=`
  • ${item}
  • `); +``` + +```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 1cb0d4758..e30b395e0 100644 --- a/snippets/call.md +++ b/snippets/call.md @@ -5,10 +5,11 @@ Given a key and a set of arguments, call them when given a context. Primarily us Use a closure to call a stored key with stored arguments. ```js -const call = (key, ...args) => context => context[ key ](...args); -/* +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 9991e279e..671b27191 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 8880c4282..1431e6b37 100644 --- a/snippets/clampNumber.md +++ b/snippets/clampNumber.md @@ -1,13 +1,16 @@ ### clampNumber -Clamps `num` within the inclusive range specified by the boundary values `a` and `b` +Clamps `num` within the inclusive range specified by the boundary values `a` and `b`. -If `num` falls within the range, return `num`. +If `num` falls within the range, return `num`. Otherwise, return the nearest number in the range. ```js const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a,b)),Math.min(a,b)); -// 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 ```