What's causing the alert to display only 1?
function test() {
var myobj = {
a : 'Cool stuff',
b : 'findtheanswer',
c : {
aa : 'Even cooler things',
bb : 'findtheanswer',
cc : {
aaa : 'The coolest item',
bbb : 'findtheanswer'
}
}
}
function countOccurrences(needle, haystack) {
var count = count || 0;
for(var i in haystack) {
if (typeof(haystack[i]) == 'object') {
countOccurrences(needle, haystack[i]);
} else {
if (needle == haystack[i]) {
count++;
}
}
}
return count;
}
alert(countOccurrences('findtheanswer', myobj));
}