Array
all
Returns true if the provided predicate function returns true for all elements in a collection, false otherwise.
Use Array.prototype.every() to test if all elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.
const all = (arr, fn = Boolean) => arr.every(fn); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Array
all
Returns
trueif the provided predicate function returnstruefor all elements in a collection,falseotherwise.Use
Array.prototype.every()to test if all elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const all = (arr, fn = Boolean) => arr.every(fn);all([4, 2, 3], x => x > 1); // true all([1, 2, 3]); // trueallEqual
Check if all elements in an array are equal.
Use
Array.prototype.every()to check if all the elements of the array are the same as the first one. Elements in the array are compared using the strict comparison operator, which does not account forNaNself-inequality.const allEqual = arr => arr.every(val => val === arr[0]); diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index 76a53ca55..045a0e15e 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -386,7 +386,7 @@ "archived": false }, "meta": { - "hash": "d0eace2a9fcd0b8e056083e8c3e92574d7da0eee62c6897f7d6c5a94e64a3841" + "hash": "e2fe0ff4736e09c616dda81905f39b89a213f4ea1d9d8fec7fb4c393a656ad72" } }, { diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 470250c5c..6ee5e5902 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -568,7 +568,7 @@ }, "meta": { "archived": false, - "hash": "d0eace2a9fcd0b8e056083e8c3e92574d7da0eee62c6897f7d6c5a94e64a3841" + "hash": "e2fe0ff4736e09c616dda81905f39b89a213f4ea1d9d8fec7fb4c393a656ad72" } }, { diff --git a/snippets/checkProp.md b/snippets/checkProp.md index 67b318e19..07de4a069 100644 --- a/snippets/checkProp.md +++ b/snippets/checkProp.md @@ -27,6 +27,7 @@ const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); + const lengthIs4 = checkProp(l => l === 4, 'length'); lengthIs4([]); // false lengthIs4([1,2,3,4]); // true diff --git a/snippets_archive/README.md b/snippets_archive/README.md index 6dae41d35..feca993a2 100644 --- a/snippets_archive/README.md +++ b/snippets_archive/README.md @@ -3,6 +3,7 @@ These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are. ## Table of Contents * [`JSONToDate`](#jsontodate) +* [`squareSum`](#squaresum) * [`binarySearch`](#binarysearch) * [`celsiusToFahrenheit`](#celsiustofahrenheit) * [`cleanObj`](#cleanobj) @@ -14,7 +15,6 @@ These snippets, while useful and interesting, didn't quite make it into the repo * [`fibonacciUntilNum`](#fibonacciuntilnum) * [`heronArea`](#heronarea) * [`howManyTimes`](#howmanytimes) -* [`httpDelete`](#httpdelete) * [`httpPut`](#httpput) * [`isArmstrongNumber`](#isarmstrongnumber) * [`isSimilar`](#issimilar) @@ -26,7 +26,7 @@ These snippets, while useful and interesting, didn't quite make it into the repo * [`removeVowels`](#removevowels) * [`solveRPN`](#solverpn) * [`speechSynthesis`](#speechsynthesis) -* [`squareSum`](#squaresum) +* [`httpDelete`](#httpdelete) --- ### JSONToDate @@ -53,6 +53,27 @@ JSONToDate(/Date(1489525200000)/); // "14/3/2017"
[⬆ Back to top](#contents) +### squareSum + +Squares each number in an array and then sums the results together. + +Use `Array.prototype.reduce()` in combination with `Math.pow()` to iterate over numbers and sum their squares into an accumulator. + +```js +const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0); +``` + +++ +Examples
+ +```js +squareSum(1, 2, 2); // 9 +``` + +
[⬆ Back to top](#contents) + ### binarySearch Use recursion. Similar to `Array.prototype.indexOf()` that finds the index of a value within an array. @@ -367,38 +388,6 @@ howManyTimes(100, -1); // Infinity
[⬆ Back to top](#contents) -### httpDelete - -Makes a `DELETE` request to the passed URL. - -Use `XMLHttpRequest` web api to make a `delete` request to the given `url`. -Handle the `onload` event, by running the provided `callback` function. -Handle the `onerror` event, by running the provided `err` function. -Omit the third argument, `err` to log the request to the console's error stream by default. - -```js -const httpDelete = (url, callback, err = console.error) => { - const request = new XMLHttpRequest(); - request.open('DELETE', url, true); - request.onload = () => callback(request); - request.onerror = () => err(request); - request.send(); -}; -``` - --- -Examples
- -```js -httpDelete('https://website.com/users/123', request => { - console.log(request.responseText); -}); // 'Deletes a user from the database' -``` - -
[⬆ Back to top](#contents) - ### httpPut Makes a `PUT` request to the passed URL. @@ -739,21 +728,32 @@ speechSynthesis('Hello, World'); // // plays the message
[⬆ Back to top](#contents) -### squareSum +### httpDelete -Squares each number in an array and then sums the results together. +Makes a `DELETE` request to the passed URL. -Use `Array.prototype.reduce()` in combination with `Math.pow()` to iterate over numbers and sum their squares into an accumulator. +Use `XMLHttpRequest` web api to make a `delete` request to the given `url`. +Handle the `onload` event, by running the provided `callback` function. +Handle the `onerror` event, by running the provided `err` function. +Omit the third argument, `err` to log the request to the console's error stream by default. ```js -const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0); +const httpDelete = (url, callback, err = console.error) => { + const request = new XMLHttpRequest(); + request.open('DELETE', url, true); + request.onload = () => callback(request); + request.onerror = () => err(request); + request.send(); +}; ```Examples
```js -squareSum(1, 2, 2); // 9 +httpDelete('https://website.com/users/123', request => { + console.log(request.responseText); +}); // 'Deletes a user from the database' ```
