function countSubstring(str, subStr) {
var strarr = str.split('');
var str1arr = [];
var index = 0, count = 0;
while(index < strarr.length){
var sub = index + subStr.length + 1 <= strarr.length ? strarr.slice(index, index+subStr.length).join('') : strarr.slice(index).join('');
/*substring with same number of characters as subStr, check if a substring equals the
subStr, increment count and remove that substring (non-overlapping occurrences)*/
if(sub == subStr){
count++;
strarr.splice(index, subStr.length);
}else{
index++;
}
}
return count;
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.