I'm just starting to learn JavaScript, but I'm afraid I'm stuck at this part. I understand the idea of this code, but I don't fully understand how it works, I would appreciate some explanation.
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);
What you have there is an algorithm, a fairly efficient way of doing a
reverse
to the elements of aarray
:Let me explain, there is the function
reverse
which does exactly the same:However, if we wanted to do a manual process, what many people would do would be something like the following:
And for them that code would make sense, however as we can see it does not give us the expected result (the resulting array does not change and is apparently the same as the original).
This is precisely why we see the following instruction in your code:
And it is that this condition is key so that we do not inadvertently do a double reverse because if we look closely at what is happening, what happens is that when we reach half or before half of the iterations of all the array, our process has theoretically finished.
But if we were to keep executing the process using all the memory spaces in the array, technically we would be first reverting the array once and then reverting it again:
As we can see, and as we said, first we reverse it and then we reverse it again, remaining as it was at the beginning.
In technical terms, we came to the conclusion that then we can NOT directly use the full size of the array to perform one
reverse
by one,array
because if we used the full size of onearray
it would happen that we would reverse the array twice, giving us an unexpected result.With this we can draw another very important conclusion, and that is that by analyzing how the algorithm works, we deduce that it is only necessary to reach half or less than half of the full path of the array to make a
reverse
, making the process even more efficient.So at this point I will explain in more detail what is happening in your code.
In this part, a cycle is declared that goes from the first element of the array to the nearest integer half of the array, eg:
if we had an array of 5 spaces the result of
Math.floor(array.length / 2)
should be2
thenMath.floor
round down:So let's remember that the loop will never go through the entire array, it will only go through half or less than half (since it is the only thing that is needed and it would be wrong to carry the process to the end).
In other words, if we analyze it well, it means that it will take the first value of the array and replace it with the value of the last element of the array.
It will then take the second value of the array and replace it with the penultimate value of the array.
For this specific case, 2 iterations are enough, so the cycle ends there, and when doing the process we will have the following:
Concluding then in a more graphic and understandable way what is happening, because in reality it is as if we were killing two birds with one stone, since we order quickly and efficiently with fewer iterations than the array itself has.