function reverseArrayInPlace(array) {
for (let i = 0; i < Math.floor(array.length / 2); i++) {
let old = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = old;
}
return array;
}
let arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
I am working on the Data Structures of the book Eloquent JavaScript. I mostly understand how this loop works which is cutting the array in half and then start swapping elements at the opposite sides. but here my problem: the first round of the loop my understanding of "array[i]" is at index 0 and "Math.floor(array.length / 2)" is at index 1. So, we set old = index 0 and then override it to "array[array.length - 1 - i]". the question is first what exactly does -i part mean and what index does this "array[array.length - 1 - i]" located at at the first round of the loop. please someone explain it to I am very visual and I can't pass it if I don’t see each statement in action.