Hello Joseph, You don’t need to go through such complicated for loop to reverse the array in place. Using the Array.splice() method you can achieve the results.
array.splice(index, deleteCount, items);
startIndex at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array (with origin -1) and will be set to 0 if absolute value is greater than the length of the array.
deleteCount An integer indicating the number of old array elements to remove.If deleteCount is omitted, or if its value is larger than array.length - start (that is, if it is greater than the number of elements left in the array, starting at start), then all of the elements from start through the end of the array will be deleted.If deleteCount is 0 or negative, no elements are removed. In this case, you should specify at least one new element (see below).
itemsThe elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.
var myArray = [1, 3, 6, 9, 5, 2, 8, 5];
var reverseArrayInPlaceFn = function (array) {
var arrLength = array.length;
for (i = 0; i < arrLength; i++) {
myArray.splice(i, 0, myArray.pop());
}
}reverseArrayInPlaceFn(myArray)
console.log(myArray);// [5, 8, 2, 5, 9, 6, 3, 1]
reverseArrayInPlaceFn(myArray)
console.log(myArray);// [1, 3, 6, 9, 5, 2, 8, 5]
