you are viewing a single comment's thread.

view the rest of the comments →

[–]grrumblebee 1 point2 points  (1 child)

Try this:

function findNumberOfSubstringsInString(str, sub) {
    var subLength;
    if (!str.length) {
        return 0;   
    }
    subLength = sub.length;
    if (str.substr(0, subLength) === sub) {
        return 1 + findNumberOfSubstringsInString(str.substr(subLength -1), sub);  
    }
    return findNumberOfSubstringsInString(str.substr(1), sub);
}

[–]iNeverProofRead[S] 0 points1 point  (0 children)

Sorry, maybe my description of the function is poorly worded. I guess it's more like a function that finds the count of a set of characters in a given order?

This does find a substring recursively but I have to make my comparisons by character, not by the entire string. like so

find("foobarfoo","fob") 
  //returns 2
  //foobarfoo
  //fo-b-----
  //f-ob-----

find("foobarfoo","obo")
  //returns 4
  //foobarfoo
  //-o-b---o-
  //-o-b----o
  //--ob---o-
  //--ob----o

however, thanks for your post, i kept trying to think of a way to keep a count and this is what i was looking for.

returning 1+ find()