Two Pointer Algorithm Javascript Substring Indexof