I have a Javascript function called reverseArray that takes an array as an argument and returns a new array that has the same values as the input array in reverse order. I want to create a function reverseArryInPlace that will change the value of the input array in reverse order.
function reverseArray(inputArray) { var outputArray = []; for (var i = inputArray.length - 1; i >= 0; i--) outputArray.push(inputArray[i]); return outputArray; } function reverseArrayInPlace(inPlaceInputArray) { inPlaceInputArray = reverseArray(inPlaceInputArray); console.log('Inside reverseArrayInPlace: ' + inPlaceInputArray); return inPlaceInputArray; } var arrayValue = [1, 2, 3, 4, 5]; reverseArrayInPlace(arrayValue); console.log('Outside reverseArrayInPlace: ' + arrayValue);
Here is the result that I get when I execute this piece of code:
Inside reverseArrayInPlace: 5,4,3,2,1 Outside reverseArrayInPlace: 1,2,3,4,5
Inside the reverseArrayInPlace function, the arrayValue variable was canceled as expected. Why, when I refer to the same variable outside the reverseArrayInPlace function, does it return to the original order?
source share