0

In this function, why this console.log is not returning the original array if I i'm not changing nothing yet?

var main =[
[2,0,0,0],   
[0,0,0,0],   
[4,0,0,0],   
[4,0,0,0]   
];

function rotateArray(){
   let original = main; 
   console.log(main); //nothing should be afecting main for the moment

   let x = 0;
  for(let u = 0; u<original.length;u++){
    let y= 3;
    for(let i = 0; i<original[u].length;i++){
        original[u][i]= main[y][x];
        y--;
    }
    x++;
    }
}

Console log output:

[4,4,0,4],
[0,0,0,4],
[0,0,0,0],
[0,0,4,4]

Why?

Cristian B_F
  • 15
  • 1
  • 5
  • How are you using `rotateArray`? – kelsny Oct 23 '22 at 15:14
  • See the answers to the [linked question](http://stackoverflow.com/questions/38660832/element-children-has-elements-but-returns-empty-htmlcollection), although you log the array before it's changed, you're not expanding that value in the console until afterward, and the console shows you the then-current value. This is one of many reasons not to stumble around in the dark with a `console.log` torch. Instead, *turn on the lights* using the debugger. :-) – T.J. Crowder Oct 23 '22 at 15:15
  • Just calling it with a button click – Cristian B_F Oct 23 '22 at 15:19

0 Answers0