Two Pointer Algorithm Javascript Substring Index