update snippets 62-78

This commit is contained in:
Stefan Feješ
2017-12-25 14:23:06 +01:00
committed by Agamemnon Zorbas
parent 32fa5a9773
commit d0a8c3b238
16 changed files with 81 additions and 36 deletions

View File

@ -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"
```

View File

@ -6,12 +6,15 @@ 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>=start && n<end);
}
```
```js
inRange(3, 2, 5) // true
inRange(3, 4) // true
inRange(2, 3, 5) // false
inrange(3, 2) // false
```

View File

@ -9,5 +9,5 @@ const initialize2DArray = (w, h, val = null) => Array(h).fill().map(() => Array(
```
```js
initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]]
initialize2DArray(2, 2, 0) -> [[0,0], [0,0]]
```

View File

@ -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]
```

View File

@ -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]
```

View File

@ -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]
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```

View File

@ -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
```