Created
March 30, 2018 10:58
-
-
Save nickihastings/dacae4483fa3f2b1b2586059f97277d1 to your computer and use it in GitHub Desktop.
Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function findElement(arr, func) { | |
var num = 0; | |
//filter the array using the provided function, this will keep anything that returns true in an array; | |
num = arr.filter(func); | |
//return the first element in the new array. | |
return num[0]; | |
} | |
findElement([1, 2, 3, 4], function(num){ return num % 2 === 0; }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment