Update snippet descriptions
This commit is contained in:
@ -5,7 +5,8 @@ tags: math,array,beginner
|
|||||||
|
|
||||||
Calculates the average of two or more numbers.
|
Calculates the average of two or more numbers.
|
||||||
|
|
||||||
- Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
- Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
- Divide the resulting array by its length.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;
|
const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;
|
||||||
|
|||||||
@ -6,12 +6,14 @@ tags: math,array,intermediate
|
|||||||
Calculates the average of an array, after mapping each element to a value using the provided function.
|
Calculates the average of an array, after mapping each element to a value using the provided function.
|
||||||
|
|
||||||
- Use `Array.prototype.map()` to map each element to the value returned by `fn`.
|
- Use `Array.prototype.map()` to map each element to the value returned by `fn`.
|
||||||
- Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
- Use `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
- Divide the resulting array by its length.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const averageBy = (arr, fn) =>
|
const averageBy = (arr, fn) =>
|
||||||
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
|
arr
|
||||||
arr.length;
|
.map(typeof fn === 'function' ? fn : val => val[fn])
|
||||||
|
.reduce((acc, val) => acc + val, 0) / arr.length;
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: deepClone
|
title: deepClone
|
||||||
tags: object,recursion,intermediate
|
tags: object,recursion,advanced
|
||||||
---
|
---
|
||||||
|
|
||||||
Creates a deep clone of an object.
|
Creates a deep clone of an object.
|
||||||
|
|||||||
@ -5,7 +5,7 @@ tags: array,intermediate
|
|||||||
|
|
||||||
Takes any number of iterable objects or objects with a `length` property and returns the longest one.
|
Takes any number of iterable objects or objects with a `length` property and returns the longest one.
|
||||||
|
|
||||||
- Use `Array.prototype.reduce()`, comparing the `length` of objects to find the longest one.
|
- Use `Array.prototype.reduce()`, comparing the length of objects to find the longest one.
|
||||||
- If multiple objects have the same length, the first one will be returned.
|
- If multiple objects have the same length, the first one will be returned.
|
||||||
- Returns `undefined` if no arguments are provided.
|
- Returns `undefined` if no arguments are provided.
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ tags: object,intermediate
|
|||||||
Returns a query string generated from the key-value pairs of the given object.
|
Returns a query string generated from the key-value pairs of the given object.
|
||||||
|
|
||||||
- Use `Array.prototype.reduce()` on `Object.entries(queryParameters)` to create the query string.
|
- Use `Array.prototype.reduce()` on `Object.entries(queryParameters)` to create the query string.
|
||||||
- Determine the `symbol` to be either `?` or `&` based on the `length` of `queryString` and concatenate `val` to `queryString` only if it's a string.
|
- Determine the `symbol` to be either `?` or `&` based on the length of `queryString` and concatenate `val` to `queryString` only if it's a string.
|
||||||
- Return the `queryString` or an empty string when the `queryParameters` are falsy.
|
- Return the `queryString` or an empty string when the `queryParameters` are falsy.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -8,7 +8,7 @@ Generates all permutations of an array's elements (contains duplicates).
|
|||||||
- Use recursion.
|
- Use recursion.
|
||||||
- For each element in the given array, create all the partial permutations for the rest of its elements.
|
- For each element in the given array, create all the partial permutations for the rest of its elements.
|
||||||
- Use `Array.prototype.map()` to combine the element with each partial permutation, then `Array.prototype.reduce()` to combine all permutations in one array.
|
- Use `Array.prototype.map()` to combine the element with each partial permutation, then `Array.prototype.reduce()` to combine all permutations in one array.
|
||||||
- Base cases are for array `length` equal to `2` or `1`.
|
- Base cases are for `Array.prototype.length` equal to `2` or `1`.
|
||||||
- ⚠️ **WARNING**: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations.
|
- ⚠️ **WARNING**: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -5,7 +5,8 @@ tags: array,random,beginner
|
|||||||
|
|
||||||
Returns a random element from an array.
|
Returns a random element from an array.
|
||||||
|
|
||||||
- Use `Math.random()` to generate a random number, multiply it by `length` and round it off to the nearest whole number using `Math.floor()`.
|
- Use `Math.random()` to generate a random number.
|
||||||
|
- Multiply it by `Array.prototype.length` and round it off to the nearest whole number using `Math.floor()`.
|
||||||
- This method also works with strings.
|
- This method also works with strings.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -6,7 +6,7 @@ tags: object,array,string,intermediate
|
|||||||
Gets the size of an array, object or string.
|
Gets the size of an array, object or string.
|
||||||
|
|
||||||
- Get type of `val` (`array`, `object` or `string`).
|
- Get type of `val` (`array`, `object` or `string`).
|
||||||
- Use `length` property for arrays.
|
- Use `Array.prototype.length` property for arrays.
|
||||||
- Use `length` or `size` value if available or number of keys for objects.
|
- Use `length` or `size` value if available or number of keys for objects.
|
||||||
- Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) created from `val` for strings.
|
- Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) created from `val` for strings.
|
||||||
- Split strings into array of characters with `split('')` and return its length.
|
- Split strings into array of characters with `split('')` and return its length.
|
||||||
|
|||||||
@ -8,7 +8,7 @@ Generates all permutations of a string (contains duplicates).
|
|||||||
- Use recursion.
|
- Use recursion.
|
||||||
- For each letter in the given string, create all the partial permutations for the rest of its letters.
|
- For each letter in the given string, create all the partial permutations for the rest of its letters.
|
||||||
- Use `Array.prototype.map()` to combine the letter with each partial permutation, then `Array.prototype.reduce()` to combine all permutations in one array.
|
- Use `Array.prototype.map()` to combine the letter with each partial permutation, then `Array.prototype.reduce()` to combine all permutations in one array.
|
||||||
- Base cases are for string `length` equal to `2` or `1`.
|
- Base cases are for `String.prototype.length` equal to `2` or `1`.
|
||||||
- ⚠️ **WARNING**: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
|
- ⚠️ **WARNING**: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -5,7 +5,7 @@ tags: string,beginner
|
|||||||
|
|
||||||
Truncates a string up to a specified length.
|
Truncates a string up to a specified length.
|
||||||
|
|
||||||
- Determine if the string's `length` is greater than `num`.
|
- Determine if `String.prototype.length` is greater than `num`.
|
||||||
- Return the string truncated to the desired length, with `'...'` appended to the end or the original string.
|
- Return the string truncated to the desired length, with `'...'` appended to the end or the original string.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -5,7 +5,7 @@ tags: string,intermediate
|
|||||||
|
|
||||||
Truncates a string up to specified length, respecting whitespace when possible.
|
Truncates a string up to specified length, respecting whitespace when possible.
|
||||||
|
|
||||||
- Determine if the string's `length` is greater or equal to `lim`. If not, return it as-is.
|
- Determine if `String.prototype.length` is greater or equal to `lim`. If not, return it as-is.
|
||||||
- Use `String.prototype.slice()` and `String.prototype.lastIndexOf()` to find the index of the last space below the desired `lim`.
|
- Use `String.prototype.slice()` and `String.prototype.lastIndexOf()` to find the index of the last space below the desired `lim`.
|
||||||
- Use `String.prototype.slice()` to appropriately truncate `str` based on `lastSpace`, respecting whitespace if possible and appending `ending` at the end.
|
- Use `String.prototype.slice()` to appropriately truncate `str` based on `lastSpace`, respecting whitespace if possible and appending `ending` at the end.
|
||||||
- Omit the third argument, `ending`, to use the default ending of `'...'`.
|
- Omit the third argument, `ending`, to use the default ending of `'...'`.
|
||||||
|
|||||||
Reference in New Issue
Block a user