From cac8a987ed4725ad093ee719bdc661c7bcde58ea Mon Sep 17 00:00:00 2001 From: Meet Zaveri Date: Sat, 16 Dec 2017 18:03:59 +0530 Subject: [PATCH] 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 +```