# 28. strStr

function strStr(haystack, needle) {
  const len = needle.length;
  for (let i = 0; i < haystack.length - len + 1; i++) {
    const str = haystack.substr(i, len);
    if (str === needle) return i;
  }

  return -1;
}

console.log(strStr("hello", "ll"));
1
2
3
4
5
6
7
8
9
10
11
Last Updated: 6/27/2023, 7:40:45 PM