I will start with an apology about my poor English. I will try to be as clear as I can. :)
I have a 3-dimensional array (just an array of 2-dimensional arrays). My goal is to take one of the 2-dimensional arrays, and rotate it 90° counterclockwise. It look like this:
[1|2|3]
[4|5|6]
[7|8|9]
I try to make it "rotate" like this:
[3|6|9]
[2|5|8]
[1|4|7]
I want to change the original array, so I figured that I need to create a copy of it so i will have a reference to work with. So, here is what I have done:
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();
The copy of the original 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 values of the array in cube
, it changes also the values of temp
. I tried to change temp = temp.concat(cube[4]);
to temp = cube[4].slice(0);
but it did not make any diffrence.
How can I fix that? Thank you all. :)