From 8ee2afab21ebb2056e760b5d1d4eb2dad0996ac4 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:03:59 +0530 Subject: [PATCH 1/7] Create First-one-to-pass-truth-test.md --- snippets/First-one-to-pass-truth-test.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/First-one-to-pass-truth-test.md diff --git a/snippets/First-one-to-pass-truth-test.md b/snippets/First-one-to-pass-truth-test.md new file mode 100644 index 000000000..7c50de1a0 --- /dev/null +++ b/snippets/First-one-to-pass-truth-test.md @@ -0,0 +1,14 @@ +### 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). + +``` +function 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 +} + +// test here +findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test +``` From f504a8cf77fd543ab508e36065ea33f3043c7dd1 Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:06:11 +0530 Subject: [PATCH 2/7] Update First-one-to-pass-truth-test.md --- snippets/First-one-to-pass-truth-test.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/snippets/First-one-to-pass-truth-test.md b/snippets/First-one-to-pass-truth-test.md index 7c50de1a0..0732eb51c 100644 --- a/snippets/First-one-to-pass-truth-test.md +++ b/snippets/First-one-to-pass-truth-test.md @@ -3,9 +3,8 @@ A function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument). ``` -function findElement(arr, func) { +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 } From d7c2e3a017997789eb6b72fed4d6569991f8980a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 14:40:03 +0200 Subject: [PATCH 3/7] Update and rename First-one-to-pass-truth-test.md to first-one-to-pass-truth-test.md --- ...pass-truth-test.md => first-one-to-pass-truth-test.md} | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) rename snippets/{First-one-to-pass-truth-test.md => first-one-to-pass-truth-test.md} (66%) diff --git a/snippets/First-one-to-pass-truth-test.md b/snippets/first-one-to-pass-truth-test.md similarity index 66% rename from snippets/First-one-to-pass-truth-test.md rename to snippets/first-one-to-pass-truth-test.md index 0732eb51c..cea62dd43 100644 --- a/snippets/First-one-to-pass-truth-test.md +++ b/snippets/first-one-to-pass-truth-test.md @@ -1,13 +1,11 @@ -### First-one-to-pass-truth-test +### 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 } - -// test here -findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test +// findElement([1, 2, 3, 4], function(num){ return num !== 2 && num !== 1 }); //3 <- first element in array to be passed truth test ``` From 1f89910782b836d1d2397ebc88a323cdaf60ee0d Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:13:15 +0530 Subject: [PATCH 4/7] Create Truth-check-objects.md --- snippets/Truth-check-objects.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/Truth-check-objects.md diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md new file mode 100644 index 000000000..042d14605 --- /dev/null +++ b/snippets/Truth-check-objects.md @@ -0,0 +1,21 @@ +### Truth-Check-Objects + +Check if the predicate (second argument) is truthy on all elements of a collection (first argument). + + + - For every object in the collection array, check the truthiness of object’s property passed in pre parameter + - Array#every method internally checks if the value returned from the callback is truthy. + - Return true if it passes for every object. Otherwise, return false. + - Also if the object is empty then it will return false + + ``` + function truthCheck(collection, pre) { + // Is everyone being true? + return collection.every(obj => obj[pre]); +} + +truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, +{"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); +// true + + ``` From 5f181e0003023ad354da906c1d23b981e3dc955d Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:21:21 +0530 Subject: [PATCH 5/7] Update Truth-check-objects.md --- snippets/Truth-check-objects.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md index 042d14605..6404a22f0 100644 --- a/snippets/Truth-check-objects.md +++ b/snippets/Truth-check-objects.md @@ -9,10 +9,7 @@ Check if the predicate (second argument) is truthy on all elements of a collecti - Also if the object is empty then it will return false ``` - function truthCheck(collection, pre) { - // Is everyone being true? - return collection.every(obj => obj[pre]); -} +truthCheck = (collection, pre) => (collection.every(obj => obj[pre])); truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); From e8838a5c9303045a82be69f47ff549c9a73b466f Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:21:32 +0530 Subject: [PATCH 6/7] Update Truth-check-objects.md --- snippets/Truth-check-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md index 6404a22f0..1c17fd949 100644 --- a/snippets/Truth-check-objects.md +++ b/snippets/Truth-check-objects.md @@ -8,7 +8,7 @@ Check if the predicate (second argument) is truthy on all elements of a collecti - Return true if it passes for every object. Otherwise, return false. - Also if the object is empty then it will return false - ``` + ```js truthCheck = (collection, pre) => (collection.every(obj => obj[pre])); truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, From 5d27345087241513d358d5c799a4e3b7b43fddea Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 18 Dec 2017 13:05:21 +0200 Subject: [PATCH 7/7] Update and rename Truth-check-objects.md to truthCheckCollection.md --- snippets/Truth-check-objects.md | 18 ------------------ snippets/truthCheckCollection.md | 10 ++++++++++ 2 files changed, 10 insertions(+), 18 deletions(-) delete mode 100644 snippets/Truth-check-objects.md create mode 100644 snippets/truthCheckCollection.md diff --git a/snippets/Truth-check-objects.md b/snippets/Truth-check-objects.md deleted file mode 100644 index 1c17fd949..000000000 --- a/snippets/Truth-check-objects.md +++ /dev/null @@ -1,18 +0,0 @@ -### Truth-Check-Objects - -Check if the predicate (second argument) is truthy on all elements of a collection (first argument). - - - - For every object in the collection array, check the truthiness of object’s property passed in pre parameter - - Array#every method internally checks if the value returned from the callback is truthy. - - Return true if it passes for every object. Otherwise, return false. - - Also if the object is empty then it will return false - - ```js -truthCheck = (collection, pre) => (collection.every(obj => obj[pre])); - -truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, -{"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex"); -// true - - ``` diff --git a/snippets/truthCheckCollection.md b/snippets/truthCheckCollection.md new file mode 100644 index 000000000..82a6d26cd --- /dev/null +++ b/snippets/truthCheckCollection.md @@ -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 +```