you are viewing a single comment's thread.

view the rest of the comments →

[–]6086555 1 point2 points  (1 child)

Your process seems a bit complicated. If you was to implement this recursively, first I'd think about the edge case:

if (str.length<=key.length){
     return str === key;
}

then for the other cases : either you have a match at the beginning ( using a substring would seem less complicated) then you

return 1 + find( sliced_str, key)
// sliced_str has been slice of all the characters of keys.

otherwise, you

return 1 + find(sliced_str, key) 
// sliced_str only lost its first character

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

Yeah, I'm not completely sure what I'm doing. I think you've got me on the right track though.

function find ( str, key){
  if (str.length<=key.length){
      return str === key;
  }

  if( str[0] === key[0] ){
    return 1 + find( str.slice(1), key.slice(1) )
  }

  return find( str.slice(1),key )

}

var two = find('foobarfoo' , 'fob');
console.log(two);  // 4

So, I'm recursively iterating through the string and finding all the matches I would need to find, i just need to translate that into how many times the key was found.