Created
February 20, 2014 21:47
-
-
Save ivanoats/9123921 to your computer and use it in GitHub Desktop.
Recursion example
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
// find the element in the array | |
var find = function(list, assertion) { | |
'use strict'; | |
var recur = function(seq) { | |
var head = seq[0], tail = seq.slice(1); | |
if ( seq.length === 0 ) return undefined; | |
else if ( assertion(head) ) return head; | |
else return recur(tail); | |
}; | |
return recur(list); | |
}; | |
var my_list = ['bob', 'jane', 'joey']; | |
console.log(find(my_list, function(element) { | |
return ( element === 'ivan'); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment