1
function reverseArray(array) {
    var reversed_array = [];
    for(var i = 0; i < array.length; i++) {
        reversed_array.unshift(array[i]);
    }

    return reversed_array;
}

function reverseArrayInPlace(array) {
    console.log(array); //[1,2,3,4,5] 

    array = reverseArray(array);
    console.log(arguments); //{0: [5, 4, 3, 2, 1]} this is good.
    console.log(array);    // [5, 4, 3, 2, 1] this is also good.
    return array;         //should return [5, 4, 3, 2, 1]   
}

console.log(reverseArray(["A", "B", "C"]));   //["C", "B", "A"]


var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);

console.log(arrayValue);  // [1, 2, 3, 4, 5] *wrong*

here is the problem I want to override the variable (arrayValue) with the return value of reverseArrayInPlace() just like array.reverse() does.

and here is a solution for the problem

function reverseArrayInPlace(array) {

  var half = Math.floor(array.length / 2);

  for(var i = 0; i < half; i++) {
    var current = array[i];
    array[i] = array[array.length - 1 - i];
    array[array.length - 1 - i] = current;
  }

  console.log(array);    // [5, 4, 3, 2, 1] this is good.
  return array;         //should return [5, 4, 3, 2, 1]
}

var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);

console.log(arrayValue);  // [5, 4, 3, 2, 1]   *it works!*

I don't understand why this solution works in replacing (arrayValue) with the new value, if both reverseArrayInPlace() functions have a return statement with the same value.

Can someone please explain I'm fairly new to programming, thanks!

Yandri
  • 41
  • 4
  • maybe you need this for custom functionality, but you are aware that js has an array reverse built in? console.log( [1, 2, 3, 4, 5].reverse() ) – yezzz May 07 '16 at 21:10

3 Answers3

3

The problem with your first code example is this: When you call reverseArrayInPlace(arrayValue); you are not assigning the result of that function call to arrayValue, so arrayValue doesn't change.

arrayValue = reverseArrayInPlace(arrayValue);

would solve that.

Now for why the reverseArrayInPlace function in your first example doesn't change the actual array elements, but the second example does:

When you pass an array as a variable to a function it is passed by reference, not by value, so you are essentially passing a pointer (array) to the function, which it is going to work with to access the actual content of the array. This reference only exists inside the scope of the function, so changing it does not affect the actual array.

array = reverseArray(array);

Here you are changing exactly this reference, but this change only applies inside the reverseArrayInPlace function and hence does not change your actual array. In your second example, however you are accessing the original array directly and changing its elements. This is why your second example works, and your first one doesn't.

Keiwan
  • 8,031
  • 5
  • 36
  • 49
  • The OP can refer to the accepted answer [here](http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference) for more info on this – It-Z May 07 '16 at 20:53
  • ohhhhhh I see, thank you so much!!I second example is accessing and changing the variables values directly(I had a feeling it was, but wasn't sure exactly how) This makes total sense thanks! – Yandri May 07 '16 at 22:13
0

You just called reverseArrayInPlace and forgot to update arrayValue:

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

on the other hand, in second example the array is updating directly where in first one, you are creating a new instance of array (when using third party function)

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
0

in your first example, you are declaring a new variable var reversed_array = []; to hold the reversed data, and then you return it from the function, but don't use the return value.

var array = reverseArray(array);

In the second example, the reverseArrayInPlace() function is actually manipulating the array that is sent into the function, this is why you can see the reversed data in the array variable you sent in.

This second example should also work without return array; as it manipulates the data directly in the variable.

MunchyYDL
  • 351
  • 1
  • 5
  • yep it does work without return and NOW i know why since the return value is not getting stored anywhere anyways, thank you :) !! – Yandri May 07 '16 at 22:24