Two Pointer Algorithm Javascript Foreach Return