0

I am trying to write code that reverses an array in place without using the reverse function (I'm busy learning JS so just doing an exercise from Eloquent JavaScript).

  function reverseArrayInPlace(arr) {
   for (let i = 0; i < arr.length; i++) {
    arr[i] = arr[(arr.length - 1) - i];
  }

  return arr;
}

This was the code that I wrote which doesn't quite work, I know this is because I have already reassigned arr[0] and arr[1] so if I call reverseArrayInPlace([1, 2, 3, 4 ,5]), [5, 4, 3, 4, 5] is returned.

This was given as the solution:

function reverseArrayInPlace(array) {
  for (let i = 0; i < Math.floor(array.length / 2); i++) {
    let old = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = old;
  }
  return array;
}

Could anyone please explain what's happening in the solution so I can better understand? Thanks :)

1
  • Consider [ 1, 2, 3, 4, 5 ] being fed into your example. After the very first iteration, your code will be working with [ 5, 2, 3, 4, 5 ]. The 1 has disappeared entirely, and you'll never be able to retrieve it to set it as the last array item, where it ought to go. The given solution swaps two numbers at a time (starting with the first and last number). After a first iteration it will produce [ 5, 2, 3, 4, 1 ]. This solves the issue of disappearing data that your solution had. Commented Jul 8, 2020 at 14:55

3 Answers 3

1

So here is what is happening in this function:

for(let i = 0; i < Math.floor(array.length / 2); i++):

they are using the Math.floor() method to make sure that you only iterate through half of the array. That is why your original solution repeated elements instead of reversing them.

let old = array[i]:

This is a temporary variable to hold the element at the current index in the loop while you swap them.

array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = old;

this swaps the elements.

which only leaves return array;

Sign up to request clarification or add additional context in comments.

Comments

0

You only need to loop over half of the array since you are swapping items; if you loop over the entire array, you will swap each item twice, not changing the array at all. In each iteration, you just copy over the value at the reflected index, instead of transposing the two values, for which you need a temporary variable to store one of the values.

Comments

0

Say you have an array [1, 2, 3, 4, 5]. The code starts from left most element, and swap it with the right most element. so you get [5, 2, 3, 4, 1]. Then it does the same for the next element in the array, swap it with the second to right element, and you'll get [5, 4, 3, 2, 1]. Math.floor(array.length) make sure that the already swapped elements don't swap again, so it will only go through the first half of the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.