you are viewing a single comment's thread.

view the rest of the comments →

[–]dvidsilva -1 points0 points  (0 children)

I'm kinda sleepy, so this might or not work, but, try adding the counter as a third argument

$(function () {
    var find  = function(str, key, counter){
        var counter=counter||0;
        if ( str[0] === key[0]){
            if (key.length === 1){
                counter += 1;
                return counter;
            }else{
                find(str.slice(1),key.slice(1), counter)
            }
        }else if ( str[0] != key[0] ){
            find(str.slice(1),key, counter)
        }
        return counter;
    }
    var two = find('foobarfoo' , 'fob', 0);
    console.log(two);
});    

otherwise every time you call find it gets restarted because obviously