Skip to content

Instantly share code, notes, and snippets.

@rickMcGavin
Last active September 7, 2016 23:38
Show Gist options
  • Save rickMcGavin/ef00594cd296735fe76f89cdd55b9e73 to your computer and use it in GitHub Desktop.
Save rickMcGavin/ef00594cd296735fe76f89cdd55b9e73 to your computer and use it in GitHub Desktop.
// 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