Yes, you’ve got it, @JustinP!
The function is working on myArray
(not really its own version). You pass myArray
in as the argument here:
console.log(reverseArrayInPlace(myArray));
which becomes the parameter you have decided to call arrayToReverse
. But it’s still exactly the same array, and you could have kept the name the same:
function reverseArrayInPlace(myArray) {...}
/* would also work as long as you also change all references
from arrayToReverse to myArray in the function body */
So whatever you decide to name the array for the purposes of manipulating it within the function (as a parameter), what you are returning to the function call and logging to the console is what is stored in the variable myArray
, and which is why both your console.log
s output identical arrays, because they are the same array value.
You can further prove to yourself that the function is actually modifying myArray
in place by doing the following:
- remove:
return arrayToReverse
;
remove theconsole.log
from the function call so you just have:
reverseArrayInPlace(myArray);
- Run the program and you will notice that
myArray
has still changed even though the function isn’t returning any value. Can you see why?
Great work and perseverance!