Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/77ec8ff9f1d1b4330300 to your computer and use it in GitHub Desktop.
Save anonymous/77ec8ff9f1d1b4330300 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Finders Keepers
// Bonfire: Finders Keepers
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-finders-keepers
// Learn to Code at Free Code Camp (www.freecodecamp.com)
//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 find(arr, func) {
var num;
// Loop thorugh the array and use the function to check
for (var a = 0; a < arr.length; a++) {
if (func(arr[a])) {
// Store the first case and break the loop
num = arr[a];
return num;
}
}
// otherwise return undefined
return num;
}
find([1, 2, 3, 4], function(num) {
return num % 2 === 0;
});
find([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