0

Refer to the given below code:
I am not able to understand that what's happening under the hood that let the confusion between the two points mentioned below:

  1. when revArrayInPlace(a) executes, it change the original value of variable a but
  2. when revArray(a) executes, it doesn't change the original value of variable a

In revArrayInPlace(a), if(x[someIndex] = someValue) replace the original value of x[someIndex]with somevalue , then logically x = newArray should also replace the original value of array xwith new value newArray when revArray(a) executes

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

function revArray(x) {
    var result = [];
    for (var i = x.length -1; i >= 0 ; i = i-1) {
        result.push(x[i]);
    }
    x = result; 
}



function revArrayInPlace(x) {
  for (var i = 0; i<Math.floor(x.length/2); i=i+1) {
    var old = x[i];
    x[i] = x[x.length - 1 - i];
    x[x.length -1 -i] = old;
  }
  return x;
}
3
  • Your "point 1" is wrong. The function does not change the value of a. It changes the contents of the array, but a remains the same; it continues to refer to the same array object. Commented Feb 27, 2017 at 16:25
  • @Pointy yeah I mean that itself Commented Feb 27, 2017 at 16:29
  • I've removed the part of my answer you folded into the question. It's important not to fold answers into the question, because people finding this later to try to use it to solve their problem are faced with an apparently non-sensical answer that just repeats part of the question (as far as they can see). Commented Feb 27, 2017 at 17:01

1 Answer 1

4

At the end of revArray, you're doing this:

x = result;

That does nothing to modify the original array or the original variable (a, I think) referring to it (but it's a common misunderstanding, you're not the only one!). All it does is modify the value in x, changing it from being a reference to the original array to being a reference to the new array. Since the next thing you do is exit the function, that doesn't accomplish anything.

In JavaScript, only values are passed into functions, not variables. So:

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

...reads the value from a and passes that value into revArray. That value tells the JavaScript engine where the array is, but it has no ongoing connection to the a variable at all. If you think about it, it has to be that way, because you can pass the result of an expression into a function; then what would assigning to the argument do? :-)

To make revArray work, return the result:

return result;

...and call it like this:

a = revArray(a);

Example:

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

function revArray(x) {
    var result = [];
    for (var i = x.length -1; i >= 0 ; i = i-1) {
        result.push(x[i]);
    }
    return result;
}

a = revArray(a);
console.log(a);

Sign up to request clarification or add additional context in comments.

7 Comments

I tried executing revArray(a) after adding return result but still it doesn't change the original value of a
you quoted "In JavaScript, only values are passed into functions, not variables" and "That value tells the JavaScript engine where the array is, but it has no ongoing connection to the a variable at all". Then how come revArrayInPlace able to change the original value of ainspite of the fact "only values are passed into functions, not variables. it has no ongoing connection to the a variable at all"
@Andrew: revArrayInPlace doesn't change the value of a at all. Remember, the value of a is where, in memory, the array is. revArrayInPlace changes the contents of the array, not the value of a.
@Andrew: Again: Changing the array isn't changing the variable at all, because the array isn't in the variable; a reference to the array (like a number telling the JavaScript engine where the array is) is in the variable. This answer of mine from a couple of years back might help.
I am greatly thankful to you for your this vital information. Every is making sense to me now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.