diff --git a/snippets/average.md b/snippets/average.md index 663a4a4bf..5facaed82 100644 --- a/snippets/average.md +++ b/snippets/average.md @@ -5,7 +5,8 @@ tags: math,array,beginner 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 const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length; diff --git a/snippets/averageBy.md b/snippets/averageBy.md index 6b299cad9..3d3eb29df 100644 --- a/snippets/averageBy.md +++ b/snippets/averageBy.md @@ -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. - 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 const averageBy = (arr, fn) => - arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) / - arr.length; + arr + .map(typeof fn === 'function' ? fn : val => val[fn]) + .reduce((acc, val) => acc + val, 0) / arr.length; ``` ```js diff --git a/snippets/deepClone.md b/snippets/deepClone.md index 41ab77dae..9bfc09926 100644 --- a/snippets/deepClone.md +++ b/snippets/deepClone.md @@ -1,6 +1,6 @@ --- title: deepClone -tags: object,recursion,intermediate +tags: object,recursion,advanced --- Creates a deep clone of an object. diff --git a/snippets/longestItem.md b/snippets/longestItem.md index 2a1f53903..c10a32fb2 100644 --- a/snippets/longestItem.md +++ b/snippets/longestItem.md @@ -5,7 +5,7 @@ tags: array,intermediate 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. - Returns `undefined` if no arguments are provided. diff --git a/snippets/objectToQueryString.md b/snippets/objectToQueryString.md index c3cae1114..7df842601 100644 --- a/snippets/objectToQueryString.md +++ b/snippets/objectToQueryString.md @@ -6,7 +6,7 @@ tags: object,intermediate 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. -- 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. ```js diff --git a/snippets/permutations.md b/snippets/permutations.md index 1289932f4..7d0c2c928 100644 --- a/snippets/permutations.md +++ b/snippets/permutations.md @@ -8,7 +8,7 @@ Generates all permutations of an array's elements (contains duplicates). - Use recursion. - 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. -- 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. ```js diff --git a/snippets/sample.md b/snippets/sample.md index 68f7414dc..843c1a0e1 100644 --- a/snippets/sample.md +++ b/snippets/sample.md @@ -5,7 +5,8 @@ tags: array,random,beginner 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. ```js diff --git a/snippets/size.md b/snippets/size.md index cd61e89be..4972a8351 100644 --- a/snippets/size.md +++ b/snippets/size.md @@ -6,7 +6,7 @@ tags: object,array,string,intermediate Gets the size of an 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 `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. diff --git a/snippets/stringPermutations.md b/snippets/stringPermutations.md index 1a7769b6e..27b669e7f 100644 --- a/snippets/stringPermutations.md +++ b/snippets/stringPermutations.md @@ -8,7 +8,7 @@ Generates all permutations of a string (contains duplicates). - Use recursion. - 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. -- 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. ```js diff --git a/snippets/truncateString.md b/snippets/truncateString.md index 5f32245a6..d1f4692af 100644 --- a/snippets/truncateString.md +++ b/snippets/truncateString.md @@ -5,7 +5,7 @@ tags: string,beginner 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. ```js diff --git a/snippets/truncateStringAtWhitespace.md b/snippets/truncateStringAtWhitespace.md index 16018075e..e7a59bbbf 100644 --- a/snippets/truncateStringAtWhitespace.md +++ b/snippets/truncateStringAtWhitespace.md @@ -5,7 +5,7 @@ tags: string,intermediate 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()` 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 `'...'`.