Find the starting indices of all occurrences of the pattern in the string The task


  Given a string and a pattern, find the starting indices of all
  occurrences of the pattern in the string. For example, given the
  string "abracadabra" and the pattern "abr", you should return [0, 7].


My solution

function findStartingIndex(T, pattern) {
  let S = T;
  const res = [];
  while(true) {
    const i = S.indexOf(pattern);
    if (i === -1) { return res; }

    S = S.substring(i + 1);
    res.push(i ? i + 1 : i);
  }
  return res;
}
Your code contains a bug.

For banana and a, it should return [1, 3, 5], but it doesn't.