const countSubstring = (str, searchStr) => {
let i = 0;
let count = 0;
while (true) {
let r = str.indexOf(searchStr, i);
if (r !== -1) {
[count, i] = [count + 1, r + 1];
} else return count;
}
}
const testStr = "hello marry i miss you marry aha marry";
console.log(countSubstring(testStr, 'marry'));