For the sake of honing my skills, I undertook a practice task to identify patterns of varying lengths within a specified string. How can this function be enhanced? What potential issues should I address in terms of optimization?
function searchPattern(pattern, str) {
var arr = [];
for (i = 0; i < str.length; i++) {
if (pattern == str.slice(i, pattern.length + i)) {
arr.push([i, pattern.length - 1 + i]);
};
};
if (!arr.length) {
return false;
} else {
return arr;
};
};
searchPattern('abfd', 'abfdffdabfdfaffab');