0

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?

Jake11
  • 743
  • 2
  • 8
  • 23
  • you may have a look here: https://stackoverflow.com/q/42581128/1447675 – Nina Scholz Nov 02 '18 at 14:56
  • Part of your problem lies in the way you are using fill. That may help you: https://stackoverflow.com/questions/37949813/array-fillarray-creates-copies-by-references-not-by-value – Karl-André Gagnon Nov 02 '18 at 14:58
  • 2
    Your outer array contains three references to the same array, so when you change a cell in one of the lines you're changing it in all of them. – Aaron Nov 02 '18 at 14:58
  • 1
    From your problem statement it doesn't look like you've got any reason to initialize your array with zeroes anyway, so just use `arr = [[],[],[]]` and you should be good – Aaron Nov 02 '18 at 15:01

3 Answers3

2

The problem is the way you created your output array:

const arr = Array(a.length).fill(Array(a.length).fill(0));

This says "Every element of arr should equal the result of Array(a.length).fill(0). That is to say, array arr has three elements, each of which is the same object.

Therefore, when you assign a value to arr[0][2], you are also assigning it to arr[1][2], etc.

You could use a loop like this to create your new matrix:

var arr = Array(a[0].length);
for (var i = 0; i < arr.length; i++)
    arr[i] = Array(a.length).fill(0);
Austin Mullins
  • 7,099
  • 2
  • 32
  • 47
2

the problem is the output array

Solution :

function rotateImage(a) {
    let arr = Array(a.length);

    for(let i =0; i< 3; i++) {
        for(let j = 0; j < a[i].length;j++ ){
                if(!arr[j]) {
                    arr[j] = Array(a[j].length)
                }
                arr[j][a[i].length-1 - i] =  a[i][j];
        }
    }
    return arr;
}
Jose Rodriguez
  • 251
  • 2
  • 7
1

This function can be written as three maps, in a one-liner:

const rotateImage = a =>  a[0].map((_, i) => a.map(r => r[i])).map(r => r.reverse())

console.log(rotateImage([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
]))

Note that this works with non-square grids as well.

Scott Sauyet
  • 44,568
  • 4
  • 43
  • 95