Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nickihastings
Created March 30, 2018 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickihastings/dacae4483fa3f2b1b2586059f97277d1 to your computer and use it in GitHub Desktop.
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).
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