Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reverseArrayInPlace() not working properly

There have been other questions about this problem, but I'd like to know why my code does not reverse the array given. Note: I am very new to coding, just teaching myself and trying to get practice for personal interest.

In the book Eloquent Javascript, we are asked to write two different functions which reverse an array. The first function, reverseArray, is supposed to output a new array which is the reverse of the given one. That one is easy enough.

The second function, reverseArrayInPlace, is supposed to change the given array so it is reversed. It is assumed that merely using the first function and then assigning its value to the first array is "cheating". Also, we cannot use the .reverse method.

Here is my attempt, and I can't figure out why it doesn't work:

var reverseArrayInPlace = function(anArray) {
    for (var i = 1; i < anArray.length; i++) {
        anArray = anArray.slice(i, i+1).concat
        (anArray.slice(0,i)).concat(anArray.slice(i+1));
    }
}

Note: I don't like how I wrote this function, but nonetheless I can't decide why it doesn't work.

Here is the test code given in the book and the target output:

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

The hint given in the book says:

"The trick is to swap the first and last elements, then the second and second-to-last, and so on. You can do this by looping over half the length of the array (use Math.floor to round down—you don’t need to touch the middle element in an array with an odd length) and swapping the element at position i with the one at position array.length - 1 - i. You can use a local variable to briefly hold on to one of the elements, overwrite that one with its mirror image, and then put the value from the local variable in the place where the mirror image used to be."

I don't like my idea, but I like this hint even less. Is there something more in the spirit of changing the array "in place" with the hint's idea than mine?

Thanks for the help. And again, we can't just use something called .reverse on the given array.


1 Answers

Although this is not the shortest solution, I've tried to write it to be easy to understand.

Basically we create 2 index pointers into the array, left and right, with left of course starting at the first element, and then right the last one.

Because we don't need to swap the middle a simple check of left < right inside a while loop will make it stop before it gets there.

We then use the tmp var, to use as a temporary placeholder while we swap the items, after which we can increase the left and decrease the right index pointers.

To make this an in-place replace, we use index array access methods, using [] instead of slice etc.

var reverseArrayInPlace = function(anArray) {
  var left = 0, right = anArray.length - 1, tmp;
  while (left < right) {
    tmp = anArray[left];
    anArray[left] = anArray[right];
    anArray[right] = tmp;    
    left ++;
    right --;
  }
}

var a = [1,2,3,4,5];

reverseArrayInPlace(a);

console.log(a);
like image 116
Keith Avatar answered Nov 19 '25 04:11

Keith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!