Merge pull request #187 from meetzaveri/patch-2
Create First-one-to-pass-truth-test.md
This commit is contained in:
11
snippets/first-one-to-pass-truth-test.md
Normal file
11
snippets/first-one-to-pass-truth-test.md
Normal file
@ -0,0 +1,11 @@
|
||||
### First one to pass truth test
|
||||
|
||||
A function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
|
||||
|
||||
```js
|
||||
findElement = (arr, func) => {
|
||||
filterArr = arr.filter(func); //filter array with the function provided
|
||||
return filterArr[0]; //return the first element that returns true, or undefined if no elements return true
|
||||
}
|
||||
// findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test
|
||||
```
|
||||
10
snippets/truthCheckCollection.md
Normal file
10
snippets/truthCheckCollection.md
Normal file
@ -0,0 +1,10 @@
|
||||
### truthCheckCollection
|
||||
|
||||
Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).
|
||||
|
||||
Use `Array.every()` to check if each passed object has the specified property and if it returns a truthy value.
|
||||
|
||||
```js
|
||||
truthCheckCollection = (collection, pre) => (collection.every(obj => obj[pre]));
|
||||
// truthCheckCollection([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}], "sex") -> true
|
||||
```
|
||||
Reference in New Issue
Block a user