I can't seem to figure out why my recursive search will not behave recursively.
Do you see what's wrong? Do I have a haystack[i] in the wrong place? Because I am not seeing it. I've tried looking through examples on this site but I can't figure out something so simple.
search = function(needle, haystack) {
len = haystack.length;
for (var i = 0; i < len; i++)
{
if (typeof haystack[i] == 'object') {
search(needle, haystack[i])
} else {
if (needle == haystack[i]) {
console.log('found');
return;
}
console.log('value: ' + haystack[i])
}
}
}
var test = [[1], [2], [3,4], [5,6]]
search(4, test)
Or see the fiddle @ http://jsfiddle.net/aniyishay/TBMmK/ (Open the Console)