Can anyone show me where I'm going wrong with this function on reversing arrays? It's an exercise from the book Eloquent JavaScript.
I've checked my variables through the console and the loop seems to be working but at the end the array indexes don't seem to get overwritten and I don't understand why!
The answer I'm looking for is:
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
"use strict";
var sampleArray = [1,3,5,7,9];
function reverseArrayInPlace (someArray) {
var MiddleIndex = (Math.floor(someArray.length/2));
for (var i = 0; i <= MiddleIndex-1; i++) {
var currentValue = someArray[i];
var mirrorIndex = someArray[someArray.length -i -1];
var temp = currentValue;
currentValue = mirrorIndex;
mirrorIndex = temp;
}
return someArray;
}
console.log(reverseArrayInPlace(sampleArray));
var temp = currentValueyou needsomeArray[i] = mirrorIndex;, etc. You also don't need currentValue, you can just dovar temp = someArray[i];. And mirrorIndex is poorly named, consider mirrorValue.