Reverse an array in place (two pointers)

Easy TimeO(n) SpaceO(1)

Given an array arr, reverse the order of its elements in place, with no second array, so [1, 2, 3, 4, 5, 6] becomes [6, 5, 4, 3, 2, 1]. Every element ends at the mirror of the position it started in, which leaves the middle element of an odd-length array exactly where it was.

Examples

Example 1

Input
arr = [1, 2, 3, 4, 5, 6]
Output
[6,5,4,3,2,1]
Six values, each ending at the mirror of where it started.

Example 2

Input
arr = [1, 2, 3]
Output
[3,2,1]
An odd length, so the 2 in the middle is its own mirror and never moves.

The Code

function reverse(arr) {
  let lo = 0;
  let hi = arr.length - 1;
  while (lo < hi) {
    const tmp = arr[lo];
    arr[lo] = arr[hi];
    arr[hi] = tmp;
    lo++;
    hi--;
  }
  return arr;
}
reverse([1, 2, 3, 4, 5, 6]);
Done
Step through reverse([1, 2, 3, 4, 5, 6]) call by call