I am trying to work through this problem and I am having trouble understanding why the function reverseArrayInPlace isn't doing what I want it to. The console.log in the function will return a reversed array, but when I return the same thing, I don't get the reversed array.
I am a beginner so please dumb down the answers alot. Thanks
function reverseArray(array){
var x = [];
for (i=array.length - 1; i>=0; i--){
x.push(array[i]);
}
return x;
}
function reverseArrayInPlace(array){
console.log(reverseArray(array));
return reverseArray(array);
}
//console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1]
Edit: Thanks for the replies. Can I get some feedback on this function:
function reverseArrayInPlace(array){
for (i=1; i<array.length; i++){
var x = array[i];
array.splice(i,1);
array.unshift(x);
}
return array;
}
It seems to be working for me. Can you see anything wrong with it?
reverseArrayInPlaceshould actually be calledcreateNewArrayWithValuesReversed(ie, the name of the method lies)