0

I am trying a JavaScript challenge. Not to use standard array reverse method but instead creating a new function to modify an array that given as argument and reverse its elements. Here is the example:

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

However, I created this function but it didn't work:

function reverseArrayInPlace(arr) {
  var newArr = [];
  for (var i = arr.length - 1; i >= 0; i--) {
    newArr.push(arr[i]);
  }
  arr = newArr; //This reverse arr successfully but won't work when called
  return arr;
}

var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [1, 2, 3, 4, 5], why? The arr variable above returned [5, 4, 3, 2, 1] but not here

This is the answer and it worked:

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

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

What is wrong with my method. What I don't get is the console.log did output the right reverse order but it will still show the original arrayValue when output. Can someone explain the difference to me?

ian0411
  • 4,115
  • 3
  • 25
  • 33
  • You just had to do `arrayValue = reverseArrayInPlace(arrayValue);` before logging it – vibhor1997a Dec 13 '17 at 16:29
  • 2
    `var newArr = [];` -> you're no longer working with the original array, rather a new object reference which `arr` is updated to point at locally within the function, but not the reference to it outside of the function. – James Thorpe Dec 13 '17 at 16:29
  • 1
    You are creating new array, you didn't changed original array... – sinisake Dec 13 '17 at 16:29
  • So this `arr = newArr;` doesn't do anything? And if I output `console.log` on both `arr` or `newArr`, they did show reversed and that is something I am very confused... – ian0411 Dec 13 '17 at 16:30
  • 1
    Possible duplicate of [Reversing an Array in Place why does it not work?](https://stackoverflow.com/questions/37093452/reversing-an-array-in-place-why-does-it-not-work) – James Thorpe Dec 13 '17 at 16:36
  • I understand this question is probably very easy to any experienced developer. But can anyone explain it why this line (`arr = newArr;`) in my code doesn't work as expected? – ian0411 Dec 13 '17 at 16:37
  • 1
    From wikipedia: In computer science, an in-place algorithm is an algorithm which transforms input using no auxiliary data structure. However a small amount of extra storage space is allowed for auxiliary variables. In-place algorithm updates input sequence only through replacement or swapping of elements. -> So you have to modify the original array passed, not to allocate a new one. As arr is inside function it is different from arr outside. The arr argument passed to function (which is a reference, not the whole thing) occupies different memory that arr outside – ilmirons Dec 13 '17 at 16:44
  • @ilmirons, I think the post you found on Wikipedia solves my question. Do you mind to add to your answer and I will mark as answer. Thank you so much for looking into that for me. – ian0411 Dec 13 '17 at 16:47
  • 1
    Sure. To go deeper you may read also https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference – ilmirons Dec 13 '17 at 16:53

2 Answers2

1

The assignment is asking to modify the array in place, not to allocate new array/reassigning reference. This means you have to modify the variable passed as argument without allocating new memory for the variable returned (so you don't have another memory reference). The value you return from your function is not allocated to the original variable as it belongs to another scope and no assignment of returned value is performed (i.e. arrayValue = reverseArrayInPlace(arrayValue))

About in place algorythm, from wikipedia: In computer science, an in-place algorithm is an algorithm which transforms input using no auxiliary data structure. However a small amount of extra storage space is allowed for auxiliary variables. In-place algorithm updates input sequence only through replacement or swapping of elements. -> So you have to modify the original array passed, not to allocate a new one. As arr is inside function it is different from arr outside. The arr argument passed to function (which is a reference, not the whole thing) occupies different memory that arr outside

ilmirons
  • 624
  • 6
  • 16
-1

Your script works. If you try to display it something like this:

<p id="result"></p>

<script>
 document.getElementById("result").innerHTML = reverseArrayInPlace(arrayValue);
</script>

it works

Here, see: https://plnkr.co/edit/GJ57go?p=preview

  • 1
    It should work [like this](https://plnkr.co/edit/xxcQKItzfUzNMQqhPdUK?p=preview) though - ie reversing the array actually in place - notice that it doesn't. – James Thorpe Dec 13 '17 at 16:40