Two Pointer Algorithm Javascript Foreach Array