General • Asked almost 6 years ago by Anonymous
This is the main discussion thread generated for Using The Two Pointer Technique.
[deleted]
let arr = [1, 2, 3, 4, 5];
let target = 7;
function two_sum(arr, target) {
  let n = 0;
  let k = 1;
  let pointerOne = arr[n];
  let pointerTwo = arr[arr.length - k];
  console.log(pointerOne, pointerTwo);
  let finalArray = [];
  while (nHey Rudy,
It's because of these lines:
let n = 0;
let k = 1;and while (n <= k) {
I believe you intended to set k to be the last index of the input array. But because it was set to 1, we evaluated while (0 <= 1), and thus only one iteration was done. If k was instead arr.length - 1 (5 - 1  or 4), you'd evaluate while (0 <= 4) and have several more iterations to move the two pointers.
Thanks Jake, yup you were right, I needed to update k to arr.length. I also updated my incrementing and decrementing counters from n and k to pointerOne and pointerTwo.
Here is the code below for those curious:
let arr = [1, 2, 3, 4, 5];
let target = 7;
function two_sum(arr, target) {
  let n = 0;
  let k = arr.length - 1;
  let pointerOne = arr[n];
  let pointerTwo = arr[k];
  let finalArray = [];
  while (n