diff --git a/snippets/JSONToDate.md b/snippets/JSONToDate.md index 311da31e7..9217b3de3 100644 --- a/snippets/JSONToDate.md +++ b/snippets/JSONToDate.md @@ -9,5 +9,8 @@ const JSONToDate = arr => { const dt = new Date(parseInt(arr.toString().substr(6))); return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`; }; -// JSONToDate(/Date(1489525200000)/) -> "14/3/2017" +``` + +```js +JSONToDate(/Date(1489525200000)/) -> "14/3/2017" ``` diff --git a/snippets/inRange.md b/snippets/inRange.md index ab4a610e7..044b32a0c 100644 --- a/snippets/inRange.md +++ b/snippets/inRange.md @@ -1,17 +1,20 @@ ### inRange -Checks if the given number falls within the given range. +Checks if the given number falls within the given range. Use arithmetic comparison to check if the given number is in the specified range. If the second parameter, `end`, is not specified, the range is considered to be from `0` to `start`. ```js -const inRange = (n, start, end = null) => { - if (end && start > end) end = [start, start = end][0]; - return (end == null) ? (n >= 0 && n < start) : (n >= start && n < end); -}; -// inRange(3, 2, 5) -> true -// inRange(3, 4) -> true -// inRange(2, 3, 5) -> false -// inrange(3, 2) -> false +const inRange = (n, start, end=null) => { + if(end && start > end) end = [start, start=end][0]; + return (end == null) ? (n>=0 && n=start && n Array(h).fill().map(() => Array( ``` ```js -initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]] +initialize2DArray(2, 2, 0) -> [[0,0], [0,0]] ``` diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 3fefca2ed..8ea87a9c4 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -8,6 +8,9 @@ You can omit `start` to use a default value of `0`. ```js const initializeArrayWithRange = (end, start = 0) => Array.from({ length: (end + 1) - start }).map((v, i) => i + start); -// initializeArrayWithRange(5) -> [0,1,2,3,4,5] -// initializeArrayWithRange(7, 3) -> [3,4,5,6,7] +``` + +```js +initializeArrayWithRange(5) -> [0,1,2,3,4,5] +initializeArrayWithRange(7, 3) -> [3,4,5,6,7] ``` diff --git a/snippets/initializeArrayWithValues.md b/snippets/initializeArrayWithValues.md index 47459fcdd..a92a09e7a 100644 --- a/snippets/initializeArrayWithValues.md +++ b/snippets/initializeArrayWithValues.md @@ -7,5 +7,8 @@ You can omit `value` to use a default value of `0`. ```js const initializeArrayWithValues = (n, value = 0) => Array(n).fill(value); -// initializeArrayWithValues(5, 2) -> [2,2,2,2,2] +``` + +```js +initializeArrayWithValues(5, 2) -> [2,2,2,2,2] ``` diff --git a/snippets/intersection.md b/snippets/intersection.md index 1c367cd92..0596a04ce 100644 --- a/snippets/intersection.md +++ b/snippets/intersection.md @@ -6,5 +6,8 @@ Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values co ```js const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); }; -// intersection([1,2,3], [4,3,2]) -> [2,3] +``` + +```js +intersection([1,2,3], [4,3,2]) -> [2,3] ``` diff --git a/snippets/isArmstrongNumber.md b/snippets/isArmstrongNumber.md index a680d607e..81f59409e 100644 --- a/snippets/isArmstrongNumber.md +++ b/snippets/isArmstrongNumber.md @@ -6,8 +6,11 @@ Convert the given number into an array of digits. Use `Math.pow()` to get the ap ```js const isArmstrongNumber = digits => - (arr => arr.reduce((a, d) => a + Math.pow(parseInt(d), arr.length), 0) == digits)((digits + '').split('')); -// isArmstrongNumber(1634) -> true -// isArmstrongNumber(371) -> true -// isArmstrongNumber(56) -> false + ( arr => arr.reduce( ( a, d ) => a + Math.pow( parseInt( d ), arr.length ), 0 ) == digits ? true : false )( ( digits+'' ).split( '' ) ); +``` + +```js +isArmstrongNumber(1634) // true +isArmstrongNumber(371) // true +isArmstrongNumber(56) // false ``` diff --git a/snippets/isArray.md b/snippets/isArray.md index f6e410719..28b80c262 100644 --- a/snippets/isArray.md +++ b/snippets/isArray.md @@ -6,6 +6,9 @@ Use `Array.isArray()` to check if a value is classified as an array. ```js const isArray = val => !!val && Array.isArray(val); -// isArray(null) -> false -// isArray([1]) -> true +``` + +```js +isArray(null) -> false +isArray([1]) -> true ``` diff --git a/snippets/isBoolean.md b/snippets/isBoolean.md index ae402c5e4..ad47fa33e 100644 --- a/snippets/isBoolean.md +++ b/snippets/isBoolean.md @@ -6,6 +6,9 @@ Use `typeof` to check if a value is classified as a boolean primitive. ```js const isBoolean = val => typeof val === 'boolean'; -// isBoolean(null) -> false -// isBoolean(false) -> true +``` + +```js +isBoolean(null) -> false +isBoolean(false) -> true ``` diff --git a/snippets/isDivisible.md b/snippets/isDivisible.md index 5adad9453..95919d69d 100644 --- a/snippets/isDivisible.md +++ b/snippets/isDivisible.md @@ -6,5 +6,8 @@ Use the modulo operator (`%`) to check if the remainder is equal to `0`. ```js const isDivisible = (dividend, divisor) => dividend % divisor === 0; -// isDivisible(6,3) -> true +``` + +```js +isDivisible(6,3) -> true ``` diff --git a/snippets/isEven.md b/snippets/isEven.md index 5dfcb414f..b2bb58b58 100644 --- a/snippets/isEven.md +++ b/snippets/isEven.md @@ -7,5 +7,8 @@ Returns `true` if the number is even, `false` if the number is odd. ```js const isEven = num => num % 2 === 0; -// isEven(3) -> false +``` + +```js +isEven(3) -> false ``` diff --git a/snippets/isFunction.md b/snippets/isFunction.md index af5960e00..eae2073e4 100644 --- a/snippets/isFunction.md +++ b/snippets/isFunction.md @@ -6,6 +6,9 @@ Use `typeof` to check if a value is classified as a function primitive. ```js const isFunction = val => val && typeof val === 'function'; -// isFunction('x') -> false -// isFunction(x => x) -> true +``` + +```js +isFunction('x') -> false +isFunction(x => x) -> true ``` diff --git a/snippets/isNumber.md b/snippets/isNumber.md index 83b33bc11..3035a0a3a 100644 --- a/snippets/isNumber.md +++ b/snippets/isNumber.md @@ -6,6 +6,9 @@ Use `typeof` to check if a value is classified as a number primitive. ```js const isNumber = val => typeof val === 'number'; -// isNumber('1') -> false -// isNumber(1) -> true ``` + +```js +isNumber('1') -> false +isNumber(1) -> true +``` \ No newline at end of file diff --git a/snippets/isPrime.md b/snippets/isPrime.md index 6f77ebd6c..2e609028b 100644 --- a/snippets/isPrime.md +++ b/snippets/isPrime.md @@ -2,7 +2,7 @@ Checks if the provided integer is a prime number. -Check numbers from `2` to the square root of the given number. +Check numbers from `2` to the square root of the given number. Return `false` if any of them divides the given number, else return `true`, unless the number is less than `2`. ```js @@ -11,6 +11,9 @@ const isPrime = num => { for (var i = 2; i * i <= boundary; i++) if (num % i == 0) return false; return num >= 2; }; -// isPrime(11) -> true -// isPrime(12) -> false +``` + +```js +isPrime(11) -> true +isPrime(12) -> false ``` diff --git a/snippets/isString.md b/snippets/isString.md index 775e050a4..923bcd174 100644 --- a/snippets/isString.md +++ b/snippets/isString.md @@ -6,6 +6,9 @@ Use `typeof` to check if a value is classified as a string primitive. ```js const isString = val => typeof val === 'string'; -// isString(10) -> false -// isString('10') -> true +``` + +```js +isString(10) -> false +isString('10') -> true ``` diff --git a/snippets/isSymbol.md b/snippets/isSymbol.md index a4ec67dc8..0baa68500 100644 --- a/snippets/isSymbol.md +++ b/snippets/isSymbol.md @@ -6,6 +6,9 @@ Use `typeof` to check if a value is classified as a symbol primitive. ```js const isSymbol = val => typeof val === 'symbol'; -// isSymbol('x') -> false -// isSymbol(Symbol('x')) -> true +``` + +```js +isSymbol('x') -> false +isSymbol(Symbol('x')) -> true ```