-
-
Save AliceWonderland/030ac064ce45be527ef82b6cce1fa4c7 to your computer and use it in GitHub Desktop.
6.8 Finder Function created by smillaraaq - https://repl.it/HGRh/3
This file contains hidden or 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
function finderFunction(searchArray, testerFunction){ | |
for(var i=0;i<searchArray.length;i++){ | |
if(testerFunction(searchArray[i])){ | |
return i; | |
} | |
} | |
return -1; | |
} | |
function finderFunctionArray(searchArray, testerFunction){ | |
var evensArray=[]; | |
for(var i=0;i<searchArray.length;i++){ | |
if(testerFunction(searchArray[i])){ | |
evensArray.push(searchArray[i]); | |
} | |
} | |
if(evensArray.length){ | |
return evensArray; | |
}else{ | |
return -1; | |
} | |
} | |
var numbers = [1, 3, 5, 64, 7, 12]; | |
var odds = [9, 13, 15, 17]; | |
function isEven(num) { return !(num % 2); }; | |
console.log(finderFunction(numbers, isEven)) // 3 | |
console.log(finderFunction(odds, isEven)) // -1 | |
console.log(finderFunctionArray(numbers, isEven)) // 3 | |
console.log(finderFunctionArray(odds, isEven)) // -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment