Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created February 20, 2014 21:47
Show Gist options
  • Save ivanoats/9123921 to your computer and use it in GitHub Desktop.
Save ivanoats/9123921 to your computer and use it in GitHub Desktop.
Recursion example
// 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