const countOccurrences = (text, str) => { let count = 0, i = -1, len = text.length - str.length; while (i++ <= len) count += text.startsWith(str, i) return count; }; console.log(countOccurrences('ababa', 'aba'));
const countOccurrences = (text, str) => { let count = 0, i = 0, len = text.length - str.length; while (i <= len) { i = text.indexOf(str, i); if(!++i) break; count++; } return count; }; console.log(countOccurrences('ababa', 'aba'));
countOccurrences('aaa', 'aa');//???