Apologies for my poor English, I will do my best to be clear. :)
I am working with a 3-dimensional array which is basically an array of 2-dimensional arrays. My task is to take one of these 2-dimensional arrays and rotate it 90° counterclockwise. Here is an example:
[1|2|3]
[4|5|6]
[7|8|9]
The rotated version should look like this:
[3|6|9]
[2|5|8]
[1|4|7]
In order to modify the original array, I decided to create a copy of it so that I have a reference to work with. Here is what I have attempted:
var temp = [];
var cube = [
// Arrays representing different elements
];
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 copied original array should be stored in temp
.
The issue arises in this line: cube[4][i][j] = temp[j][2-i];
. It not only changes the values in the cube
array but also affects the values in temp
. I attempted replacing temp = temp.concat(cube[4]);
with temp = cube[4].slice(0);
without success.
If you could advise me on how to address this problem, I would greatly appreciate it. Thank you all. :)