Last active
September 7, 2016 23:38
-
-
Save rickMcGavin/ef00594cd296735fe76f89cdd55b9e73 to your computer and use it in GitHub Desktop.
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
// freecodecamp | |
// intermediate algorithm scripting | |
// finders keepers | |
function findElement(arr, func) { | |
// create empty second array | |
var arr2 = []; | |
// filter through array | |
arr.filter(function(item) { | |
// if item in array is true with provided function | |
if (func(item)) { | |
// push item in to second array | |
arr2.push(item); | |
} | |
}); | |
// sort the items in the second array from smallest to highest | |
arr2.sort(function(a, b) { | |
return a - b; | |
}); | |
// return the second array at index 0 to return the desired result | |
return arr2[0]; | |
} | |
console.log(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