Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
Finding value in an array that passes function test
node v0.12.18
version:
master
endpoint
share
tweet
// for loop style function find(arr, func) { var num = 0; for (var i =0; i<arr.length; i++) { if ( func( arr[i] ) ) { break; } } return arr[i]; } find([1, 2, 3, 4], function(num){ return num % 2 === 0; });
http://freecodecamp.com/challenges/bonfire-finders-keepers
function find(arr, func) { var num = 0; arr.some( function( curr, index, aArray ) { if ( func( aArray[index] ) ) { num=index; } }); return num; } find([1, 2, 3, 4], function(num){ return num % 2 === 0; });
Loading…
no comments
sign in
to comment