1

I am working through the exercises in Eloquent JavaScript by Marijn Haverbeke and am working on the following problem:

"Write a function reverseArrayInPlace that modifies the array given as an argument in order to reverse its elements. Do not use the standard reverse method"

The solution given is as follows:

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

The test case given is as follows:

var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]

I understand that it is using the midpoint of the array as a rotation point, but can someone please give me a step by step explanation?

2

2 Answers 2

4

Taking line by line:

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

loops the array from 0 to the half of the array (rounded to lower number), increases i by one each time.

var old = array[i];

temporarely stores the current value of the array at the position i in the variable old.

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

sets the value of the i position to the value of the last element of the array minus the current i.

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

sets the value of the last element of the array minus the current i to the previous value (stored in the old variable).

In a nutshell, here is what's happening in a practical situation:

old will store the current looped value. Such value will be replaced to the last value MINUS the current index. Basically, when i is 0, array[0] becomes array[4] and vice-versa. when i is 1 array[1] will become array[3] and vice versa. the [array.length - 1 - i] is necessary because indexes in array starts from 0, so array.length - 1 is the last element of array, while -i moves the offset by i, which makes it possible to switch the value of the first element with the last one and vice-versa.

So where is the deal instead of doing a regular for loop?

Well, the trick is that it will take half the time and will replace two values at a time instead of looping the whole array and replacing the values one at a time.

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

Comments

4
function reverseArrayInPlace(array) {
  //iterate thru half of original array
  for (var i = 0; i < Math.floor(array.length / 2); i++) {
    var old = array[i]; //cache original i value
    array[i] = array[array.length - 1 - i]; //set i value to its "opposite" from end of array
    array[array.length - 1 - i] = old; //set "opposite" to be original i value
  }
  return array;
}

As you iterate thru 1/2 of the array, you swap the 2 values that are equal in distance from the front and end of the arrays. For example:

Original Array: [1, 2, 3, 4, 5]

Step i = 0: [5, 2, 3, 4, 1] (1 and 5 are swapped)

Step i = 1: [5, 4, 3, 2, 1] (2 and 4 are swapped)

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.