diff --git a/snippets/JSONToFile.md b/snippets/JSONToFile.md index 984e516b6..2e88f336d 100644 --- a/snippets/JSONToFile.md +++ b/snippets/JSONToFile.md @@ -6,6 +6,9 @@ Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` ```js const fs = require('fs'); -const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)); -// JSONToFile({test: "is passed"}, 'testJsonFile') -> writes the object to 'testJsonFile.json' +const JSONToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2)) +``` + +```js +JSONToFile({test: "is passed"}, 'testJsonFile') // writes the object to 'testJsonFile.json' ``` diff --git a/snippets/last.md b/snippets/last.md index 481e2d6e0..f26427591 100644 --- a/snippets/last.md +++ b/snippets/last.md @@ -6,5 +6,8 @@ Use `arr.length - 1` to compute the index of the last element of the given array ```js const last = arr => arr[arr.length - 1]; -// last([1,2,3]) -> 3 +``` + +```js +last([1,2,3]) -> 3 ``` diff --git a/snippets/lcm.md b/snippets/lcm.md index ef8ea7fdc..43e65c12f 100644 --- a/snippets/lcm.md +++ b/snippets/lcm.md @@ -10,5 +10,8 @@ const lcm = (x, y) => { const gcd = (x, y) => !y ? x : gcd(y, x % y); return Math.abs(x * y) / (gcd(x, y)); }; -// lcm(12,7) -> 84 +``` + +```js +lcm(12,7) -> 84 ``` diff --git a/snippets/mapObject.md b/snippets/mapObject.md index f9d65ee32..3eebc515f 100644 --- a/snippets/mapObject.md +++ b/snippets/mapObject.md @@ -6,9 +6,10 @@ Use an anonymous inner function scope to declare an undefined memory space, usin ```js const mapObject = (arr, fn) => - (a => (a = [arr, arr.map(fn)], a[0].reduce((acc, val, ind) => (acc[val] = a[1][ind], acc), {})))(); -/* + (a => (a = [arr, arr.map(fn)], a[0].reduce( (acc,val,ind) => (acc[val] = a[1][ind], acc), {}) )) ( ); +``` + +```js const squareIt = arr => mapObject(arr, a => a*a) squareIt([1,2,3]) // { 1: 1, 2: 4, 3: 9 } -*/ ``` diff --git a/snippets/median.md b/snippets/median.md index c7eea609c..e472e7c0d 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -10,6 +10,9 @@ const median = arr => { const mid = Math.floor(arr.length / 2), nums = [...arr].sort((a, b) => a - b); return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; }; -// median([5,6,50,1,-5]) -> 5 -// median([0,10,-2,7]) -> 3.5 +``` + +```js +median([5,6,50,1,-5]) -> 5 +median([0,10,-2,7]) -> 3.5 ``` diff --git a/snippets/negate.md b/snippets/negate.md index 5f14e4a0f..739ad97c1 100644 --- a/snippets/negate.md +++ b/snippets/negate.md @@ -6,6 +6,9 @@ Take a predicate function and apply `not` to it with its arguments. ```js const negate = func => (...args) => !func(...args); -// filter([1, 2, 3, 4, 5, 6], negate(isEven)) -> [1, 3, 5] -// negate(isOdd)(1) -> false +``` + +```js +filter([1, 2, 3, 4, 5, 6], negate(isEven)) -> [1, 3, 5] +negate(isOdd)(1) -> false ``` diff --git a/snippets/nthElement.md b/snippets/nthElement.md index be1da2ce0..9c1fbf387 100644 --- a/snippets/nthElement.md +++ b/snippets/nthElement.md @@ -7,7 +7,10 @@ If the index is out of bounds, return `[]`. Omit the second argument, `n`, to get the first element of the array. ```js -const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0]; -// nthElement(['a','b','c'],1) -> 'b' -// nthElement(['a','b','b'],-3) -> 'a' +const nthElement = (arr, n=0) => (n>0? arr.slice(n,n+1) : arr.slice(n))[0]; +``` + +```js +nthElement(['a','b','c'],1) // 'b' +nthElement(['a','b','b'],-3) // 'a' ``` diff --git a/snippets/objectFromPairs.md b/snippets/objectFromPairs.md index ebdacb92c..2ed208f44 100644 --- a/snippets/objectFromPairs.md +++ b/snippets/objectFromPairs.md @@ -6,5 +6,8 @@ Use `Array.reduce()` to create and combine key-value pairs. ```js const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {}); -// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} +``` + +```js +objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2} ``` diff --git a/snippets/objectToPairs.md b/snippets/objectToPairs.md index 7a6079650..6ea5fc051 100644 --- a/snippets/objectToPairs.md +++ b/snippets/objectToPairs.md @@ -6,5 +6,8 @@ Use `Object.keys()` and `Array.map()` to iterate over the object's keys and prod ```js const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); -// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]]) +``` + +```js +objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]]) ``` diff --git a/snippets/orderBy.md b/snippets/orderBy.md index c80b3667f..ab5581dc3 100644 --- a/snippets/orderBy.md +++ b/snippets/orderBy.md @@ -16,10 +16,11 @@ const orderBy = (arr, props, orders) => return acc; }, 0) ); -/* +``` + +```js const users = [{ 'name': 'fred', 'age': 48 },{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 },{ 'name': 'barney', 'age': 34 }]; orderby(users, ['name', 'age'], ['asc', 'desc']) -> [{name: 'barney', age: 36}, {name: 'barney', age: 34}, {name: 'fred', age: 48}, {name: 'fred', age: 40}] orderby(users, ['name', 'age']) -> [{name: 'barney', age: 34}, {name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}] -*/ ``` diff --git a/snippets/palindrome.md b/snippets/palindrome.md index cca1bb729..820162617 100644 --- a/snippets/palindrome.md +++ b/snippets/palindrome.md @@ -10,5 +10,8 @@ const palindrome = str => { const s = str.toLowerCase().replace(/[\W_]/g,''); return s === s.split('').reverse().join(''); } -// palindrome('taco cat') -> true - ``` +``` + +```js +palindrome('taco cat') -> true +``` diff --git a/snippets/percentile.md b/snippets/percentile.md index 7485494e2..5287521b0 100644 --- a/snippets/percentile.md +++ b/snippets/percentile.md @@ -7,5 +7,8 @@ Use `Array.reduce()` to calculate how many numbers are below the value and how m ```js const percentile = (arr, val) => 100 * arr.reduce((acc,v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length; -// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55 - ``` +``` + +```js +percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55 +``` diff --git a/snippets/pick.md b/snippets/pick.md index 668c9d30c..0bfeba286 100644 --- a/snippets/pick.md +++ b/snippets/pick.md @@ -7,5 +7,8 @@ Use `Array.reduce()` to convert the filtered/picked keys back to an object with ```js const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {}); -// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 } +``` + +```js +pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 } ``` diff --git a/snippets/pipeFunctions.md b/snippets/pipeFunctions.md index 04c9f3ee1..99970bae0 100644 --- a/snippets/pipeFunctions.md +++ b/snippets/pipeFunctions.md @@ -7,10 +7,11 @@ The first (leftmost) function can accept one or more arguments; the remaining fu ```js const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args))); -/* +``` + +```js const add5 = x => x + 5 const multiply = (x, y) => x * y const multiplyAndAdd5 = pipeFunctions(multiply, add5) multiplyAndAdd5(5, 2) -> 15 -*/ ``` diff --git a/snippets/powerset.md b/snippets/powerset.md index 09b233212..dc91406e9 100644 --- a/snippets/powerset.md +++ b/snippets/powerset.md @@ -7,5 +7,8 @@ Use `Array.reduce()` combined with `Array.map()` to iterate over elements and co ```js const powerset = arr => arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]); -// powerset([1,2]) -> [[], [1], [2], [2,1]] +``` + +```js +powerset([1,2]) -> [[], [1], [2], [2,1]] ``` diff --git a/snippets/primes.md b/snippets/primes.md index 109192e7c..87ac21a70 100644 --- a/snippets/primes.md +++ b/snippets/primes.md @@ -1,4 +1,4 @@ -### primes +### primes Generates primes up to a given number, using the Sieve of Eratosthenes. @@ -6,11 +6,14 @@ Generate an array from `2` to the given number. Use `Array.filter()` to filter o ```js const primes = num => { - let arr = Array.from({length: num - 1}).map((x, i) => i + 2), - sqroot = Math.floor(Math.sqrt(num)), - numsTillSqroot = Array.from({length: sqroot - 1}).map((x, i) => i + 2); - numsTillSqroot.forEach(x => arr = arr.filter(y => ((y % x) !== 0) || (y == x))); + let arr = Array.from({length:num-1}).map((x,i)=> i+2), + sqroot = Math.floor(Math.sqrt(num)), + numsTillSqroot = Array.from({length:sqroot-1}).map((x,i)=> i+2); + numsTillSqroot.forEach(x => arr = arr.filter(y => ((y%x)!==0)||(y==x))); return arr; -}; -// primes(10) -> [2,3,5,7] +} +``` + +```js +primes(10) // [2,3,5,7] ``` diff --git a/snippets/promisify.md b/snippets/promisify.md index 80a7d48d0..4769fa5e9 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -14,6 +14,9 @@ const promisify = func => func(...args, (err, result) => err ? reject(err) : resolve(result)) ); -// const delay = promisify((d, cb) => setTimeout(cb, d)) -// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s +``` + +```js +const delay = promisify((d, cb) => setTimeout(cb, d)) +delay(2000).then(() => console.log('Hi!')) -> // Promise resolves after 2s ``` diff --git a/snippets/pull.md b/snippets/pull.md index 3848e27dd..52e0f3ea6 100644 --- a/snippets/pull.md +++ b/snippets/pull.md @@ -14,12 +14,14 @@ const pull = (arr, ...args) => { arr.length = 0; pulled.forEach(v => arr.push(v)); }; - -// let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c']; -// pull(myArray1, 'a', 'c'); -// console.log(myArray1) -> [ 'b', 'b' ] - -// let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c']; -// pull(myArray2, ['a', 'c']); -// console.log(myArray2) -> [ 'b', 'b' ] +``` + +```js +let myArray1 = ['a', 'b', 'c', 'a', 'b', 'c']; +pull(myArray1, 'a', 'c'); +console.log(myArray1) -> [ 'b', 'b' ] + +let myArray2 = ['a', 'b', 'c', 'a', 'b', 'c']; +pull(myArray2, ['a', 'c']); +console.log(myArray2) -> [ 'b', 'b' ] ```