Created
April 22, 2016 00:10
-
-
Save notsoluckycharm/69a0141fa6801674507bab99c52f05ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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