I'm self-studying data structures and am clearly not grasping how referencing works.
I'm trying to reverse an array WITHOUT creating a new array. So I think I need to store one of the values in a local variable (x in this case) but even so, it's not updating.
function reverseArrayInPlace(arr){
for (var i = 0; i < Math.floor(arr.length / 2); i++){
let x = i;
arr[arr.length - 1 - i] = arr[i];
arr[i] = arr[x];
}
return arr;
}
let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
My expected result should just be any array reversed.
// → [5, 4, 3, 2, 1]
But I'm just getting
// → [1, 2, 3, 4, 5]