Skip to content

Instantly share code, notes, and snippets.

@notsoluckycharm
Created April 22, 2016 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notsoluckycharm/69a0141fa6801674507bab99c52f05ac to your computer and use it in GitHub Desktop.
Save notsoluckycharm/69a0141fa6801674507bab99c52f05ac to your computer and use it in GitHub Desktop.
/** Find the number of occurances of a substring within another string, occurances do not need to be
* in immediate sequence. for example the pattern ab will match with (a)(b)xcy(a)z(b) for an answer of 3
*
*/
var strPos = function( string, substring ){
var count = 0;
if( substring.length > 0 ){
var pos = 0;
string.split('').forEach(function(character){
pos += 1;
if( character == substring[0] ) {
count += strPos (
string.slice(
pos,
string.length
), substring.slice(
1,
substring.length
)
);
}
})
} else {
count += 1
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment