-
-
Save nirajkrz/b9db322a80ec010213cc to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Finders Keepers
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
// 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