I have following problem:
// Reverse Array
Write a function that accepts an array and reverses that array in place. The behavior should mimic the behavior of the native .reverse() array method. However, your reverse function should accept the array to operate on as an argument, rather than being invoked as a method on that array.
Do not use the native .reverse() method in your own implementation.
I tried the following code:
let myArray = [1, 2, 3, 4];
function reverse(myArray) {
let newArray = [];
// pop all of elements from roginal array, and store in new array
for (i=myArray.length-1; i>=0; i--){
newArray.push(myArray[i])
console.log(newArray)
}
while (newArray.length){
myArray.unshift(newArray)
}
return myArray;
}
reverse(myArray);
console.log(myArray) // expected output is [4, 3, 2, 1]
My code just keeps running and no console.log output is produced. Notice I want the reverse done to the input array argument.
What am I doing wrong? Also, what does while (newArray.length) mean / what is it doing conceptually?
while (newArray.length)changes the length ofnewArray