0

I can reverse loop using for loop and pop/shift BUT I want to reverse an array with the help of for loop only. So I tried,

let array = [1,2,3,4,5,6,7];
function reverseArray(array){
    for(let i=0; i<array.length; i++){
        let temp = array[i];
        array[(array.length-1)-i] = array[i];
        array[i] = temp;
        //[ array[i], array[(array.length-1)-i] ] = [ array[(array.length-1)-i], array[i] ]
    }
    console.log(array)
}

reverseArray(array);

You can also see the destructuring method used in the comment, it also dont work. I want to use in place array only, dont want to create another array for storage. But I am not getting reversed string, can you please point out my mistake?

Mohit M
  • 7
  • 2

0 Answers0