I am writing a short program that rotates a given NxN matrix the following way:
┌ ┐ ┌ ┐
|1 2 3| |4 1 2|
|4 5 6| -> |7 5 3|
|7 8 9| |8 9 6|
└ ┘ └ ┘
var a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
for (var i = 0; i < a.length; i++) {
for (var j = 0; j < a[i].length; j++) {
document.write(a[i][j]);
}
document.write('<br>');
};
I got the loop sorted and the array but I cannot figure out how to rotate it clockwise. It is really becoming a headache.
All help is appreciated. :)