Two Pointer Algorithm Javascript Foreach Element