I will begin by apologizing for my poor English. I will try to be as clear as possible. :)
I have a 3-dimensional array (just an array of two-dimensional arrays). My goal is to take one of the two-dimensional arrays and rotate it 90 Β° counterclockwise. It looks like this:
[1|2|3] [4|5|6] [7|8|9]
I am trying to make it βspinβ as follows:
[3|6|9] [2|5|8] [1|4|7]
I want to change the original array, so I decided that I need to create a copy of it, so I will have a link to work. So here is what I did:
var temp = []; var cube = [ [ ['A', 'A', 'A'], ['A', 'A', 'A'], ['A', 'A', 'A'] ], [ ['B', 'B', 'B'], ['B', 'B', 'B'], ['B', 'B', 'B'] ], [ ['C', 'C', 'C'], ['C', 'C', 'C'], ['C', 'C', 'C'] ], [ ['D', 'D', 'D'], ['D', 'D', 'D'], ['D', 'D', 'D'] ], [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ], [ ['F', 'F', 'F'], ['F', 'F', 'F'], ['F', 'F', 'F'] ] ]; function CCW() { temp = temp.concat(cube[4]); for(var i = 0; i < 3; i++) for(var j = 0; j < 3; j++) cube[4][i][j] = temp[j][2-i]; } CCW();
A copy of the source array should be in temp .
Now the problem is in this line: cube[4][i][j] = temp[j][2-i]; . Instead of changing only the array values ββin cube , it also changes the temp values. I tried changing temp = temp.concat(cube[4]); on temp = cube[4].slice(0); but he made no difference.
How can i fix this? Thank you all.:)