function reverseArray(array) { var arrayTemp = array; var arrayResult = []; var arrayLength = array.length for (var i = 0; i <= arrayLength - 1; i++) { arrayResult.push(arrayTemp.pop()) } return arrayResult } function reverseArrayInPlace(array) { var arrayTemp = array; var arrayResult = []; var arrayLength = array.length for (var i = 0; i <= arrayLength - 1; i++) { arrayResult.push(arrayTemp.pop()) } array = arrayResult; return array; } var arrayValue = [1, 2, 3]; reverseArrayInPlace(arrayValue) console.log(arrayValue) 

It is necessary to write a function that expands the array (I know about the reverse method). The first (reverseArray) only returns the modified array, and the second (reverseArrayInPlace) should change the original array to the reverse. Why does this code return just an empty array? Where is the mistake?

  • This line is array = arrayResult; you change the local variable (the formal parameter of the function), but not the original array. - smellyshovel
  • @Igor and what about .pop wrong? 'arrayResult.push (arrayTemp.pop ())' transfers the value from the end of the original array to the end of the new one. I don’t quite understand how at the end of the function you can assign the new value 'arrayResult' to the original array - Jaycobe
  • @Jaycobe None. Therefore, the correct decision is in my answer. And your two functions are the same. - Igor

2 answers 2

 function reverseArrayInPlace(array) { var arrayTemp = array; var arrayResult = []; var arrayLength = array.length for (var i = 0; i <= arrayLength - 1; i++) { arrayResult.push(arrayTemp.pop()) } array = arrayResult; return array; } var arrayValue = [1, 2, 3]; reverseArrayInPlace(arrayValue) console.log(arrayValue) 

In fact, your code works and works correctly. To verify this, let's output the value returned by the function instead of outputting the value of the source array:

 function reverseArrayInPlace(array) { var arrayTemp = array; var arrayResult = []; var arrayLength = array.length for (var i = 0; i <= arrayLength - 1; i++) { arrayResult.push(arrayTemp.pop()) } array = arrayResult; return array; } var arrayValue = [1, 2, 3]; let res = reverseArrayInPlace(arrayValue); console.log(res); // [3, 2, 1] => работает! 

The problem is that inside the function, you override the variable array . This variable initially points to the source array arrayValue , which you are trying to change "in place". You can verify this by adding console.log(array === arrayValue); which will print true .

This is because the arrays are transmitted "by reference", and not by copying. Therefore, you can freely change the values ​​of the elements of the variable array , thereby changing the values ​​of the same elements of the variable arrayValue outside of this function.

However, here is this line:

 array = arrayResult; 

You simply override the value of the variable array . Therefore, this variable no longer points to the same array that was originally ( arrayValue ). That is the problem.

And an empty array is displayed because you "cut out" all the elements from the original array with this construction:

 arrayTemp.pop() 

At the same time arrayTemp === array === arrayValue .


What to do?

First of all, do not override the value of the variable array . Second, do not cut elements from the original array. Thirdly, since it is impossible to override a variable referring to the original array for an on-site change, it is necessary:

  1. Create a copy of the original array (just assign it to a new variable, as you can see, cannot)
  2. Work with a copy (only if you want to leave the algorithm with pop and push , but in general it is completely optional)
  3. Alternately redefine the elements of the original array with the elements from the processed copy.
  4. Do not return anything from the function (although this is optional and will not break anything; it is just accepted).

The working algorithm is described in its answer @Igor, you can see there.

  • The function does not work correctly. Not only that the expanded array is a completely new object, it also clears the original array. - Igor
  • @Igor the author asked the question "what is the error?" I answered his question. And about the fact that it clears the array, I also indicated (see the last edit). I meant that the algorithm with pop and push is basically working. But the fact that it is not suitable in this particular situation (to change the value in place) is yes. - smellyshovel
  • @Igor you gave the author a working algorithm, and I described to him why the one that he implemented does not work. I see no reason to minus. - smellyshovel
  • @smellyshovel ok, that is, the whole problem is that the contents of the array ultimately does not change. Based on this, I added another function to the function that fills the original array with values ​​from arrayResult : for(let i = 0; i <= arrayLength - 1; i++) { array.push(arrayResult.shift()) } . Of course, this is not the most rational decision (in my opinion, it was provided by @Igor), but at least I managed to understand what the problem is and come up with a solution. Thanks for the help) - Jaycobe
  • @Jaycobe, select the answer that best suited you and tick it off with a decision (check mark to the left of the answer). - smellyshovel

 function reverseArrayInPlace(array) { for (var i = 0; i < array.length / 2; i++) { var temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } } var arrayValue = [1, 2, 3]; reverseArrayInPlace(arrayValue); console.log(arrayValue); 

  • I think you are a little wrong. The author of the question worker. It simply incorrectly redefines the value. Try running the example that he gave, but instead of outputting the value of the arrayValue, output the value of what the function returns. You will see that the algorithm works correctly. - smellyshovel
  • one
    @smellyshovel If the function is called ...InPlace , then it must "in-place" expand the array. - Igor
  • So that's the point. See, there the array inside the function points to the arrayValue outside of it exactly as long as it does not override the array variable. - smellyshovel
  • And therefore, it does not change the value of arrayValue , but returns a new one, which is lost (because it does not output it and does not save any into the variable). - smellyshovel
  • And you, in your implementation of array do not override functions, but work with its elements, thereby changing the value of arrayValue outside the function. - smellyshovel