I am trying to rotate array clockwise for example from:
[[1,2,3],
[4,5,6],
[7,8,9]]
make:
[[7,4,1],
[8,5,2],
[9,6,3]]
so I written a function for that its looks as below:
function rotateImage(a) {
const arr = Array(a.length).fill(Array(a.length).fill(0));
for(let i =0; i< a.length; i++) {
for(let j = 0; j < a[i].length;j++ ){
console.log(i,j);
arr[j][a[i].length-1 - i] = a[i][j];
console.log(arr)
}
}
return arr;
}
here are logs i get:
0 0
[ [ 0, 0, 1 ], [ 0, 0, 1 ], [ 0, 0, 1 ] ]
0 1
[ [ 0, 0, 2 ], [ 0, 0, 2 ], [ 0, 0, 2 ] ]
0 2
[ [ 0, 0, 3 ], [ 0, 0, 3 ], [ 0, 0, 3 ] ]
1 0
[ [ 0, 4, 3 ], [ 0, 4, 3 ], [ 0, 4, 3 ] ]
1 1
[ [ 0, 5, 3 ], [ 0, 5, 3 ], [ 0, 5, 3 ] ]
1 2
[ [ 0, 6, 3 ], [ 0, 6, 3 ], [ 0, 6, 3 ] ]
2 0
[ [ 7, 6, 3 ], [ 7, 6, 3 ], [ 7, 6, 3 ] ]
2 1
[ [ 8, 6, 3 ], [ 8, 6, 3 ], [ 8, 6, 3 ] ]
2 2
[ [ 9, 6, 3 ], [ 9, 6, 3 ], [ 9, 6, 3 ] ]
so when i have i=0 and j=0, My code should turn to something like this:
arr[0][2]= a[0][0];
// so I should get
[ [ 0, 0, 1 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ]
// insted of
[ [ 0, 0, 1 ], [ 0, 0, 1 ], [ 0, 0, 1 ] ]
can somebody explain for me what is going on here?