最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Eloquent JavaScript reversing an array in place explanation - Stack Overflow

matteradmin16PV0评论

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?

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?

Share Improve this question asked Dec 29, 2016 at 22:03 CharStarCharStar 4271 gold badge6 silver badges26 bronze badges 2
  • All you need to know is what is Math.floor(), the for loop and array.length in JS and the rest is like a pseudo code. BTW you can always reverse an array with the Array.prototype.reverse() built in array method. – Redu Commented Dec 29, 2016 at 22:32
  • Possible duplicate of How do I reverse an int array in Java? – Jmills Commented Dec 30, 2016 at 1:19
Add a ment  | 

2 Answers 2

Reset to default 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] bees array[4] and vice-versa. when i is 1 array[1] will bee 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.

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)

Post a comment

comment list (0)

  1. No comments so far