skip to Main Content

I am trying to reverse an array using javascript recursion but its not working. Need help what I am missing here.

function reverseArrayHelper(left, right, arr) {
  if (left >= right) return; // base condition: if l an r collide then only return

  // do the small task: swapping left with right
  let temp = arr[left];
  arr[left] = arr[right];
  arr[right] = temp;
  // arr[left], arr[right] = arr[right], arr[left]

  return reverseArrayHelper(left + 1, right - 1, arr); // ask recursion to do the remaining task
}

function reverseArray(arr, m) {
  return reverseArrayHelper(m + 1, arr.length - 1, arr);
}

console.log(reverseArray([1, 2, 3, 4, 5, 6], 3));

3

Answers


  1. If you are worried about the response is undefined, you have to return the array here if (left >= right) return arr;. Since you were not returning a value in this line, the response was undefined.

    But the logic for the reverse is up to you.

    function reverseArrayHelper(left, right, arr) {
        if (left >= right) return arr; // base condition: if l an r collide then only return
    
        // do the small task: swapping left with right
        let temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        // arr[left], arr[right] = arr[right], arr[left]
    
        return reverseArrayHelper(left + 1, right - 1, arr); // ask recursion to do the remaining task
    }
    
    function reverseArray(arr, m) {
        return reverseArrayHelper(m + 1, arr.length - 1, arr);
    }
    
    console.log(reverseArray([1, 2, 3, 4, 5, 6], 3));
    Login or Signup to reply.
  2. The issue with the code is that you are not returning the modified array from the reverseArray function.

    function reverseArrayHelper(left, right, arr) {
      if (left >= right) return arr;
    
      let temp = arr[left];
      arr[left] = arr[right];
      arr[right] = temp;
    
      return reverseArrayHelper(left + 1, right - 1, arr);
    }
    
    function reverseArray(arr, m) {
      return reverseArrayHelper(m + 1, arr.length - 1, arr);
    }
    // This code reverses all elements after the 4th element (after index 3)
    console.log(reverseArray([1, 2, 3, 4, 5, 6], 3));

    In the reverseArrayHelper function, I added arr as the base case return value. This ensures that the modified array is returned all the way up the recursive calls.

    With this code, to reverse the entire array you should:

    console.log(reverseArray([1, 2, 3, 4, 5, 6], -1));
    
    Login or Signup to reply.
  3. you don’t need to find the mid point. I’ve fixed your code below. You should return the array in base condition.

    function reverseArray(arr, left = 0, right = arr.length - 1) {
      if (left >= right) {
        return arr;
      }
    
      const temp = arr[left];
      arr[left] = arr[right];
      arr[right] = temp;
    
      return reverseArray(arr, left + 1, right - 1);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search