While I am aware there are simpler methods to reverse arrays in JavaScript, I am seeking assistance to fully understand how this particular reverse method works.
Your help in clarifying this is greatly appreciated.
function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
console.log(array[array.length - 1 - i])
let old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}
console.log(reverseArrayInPlace([1, 2, 3, 4])); // 4, 3, 2, 1