Two Pointer Algorithm Javascript Code