好的,我想实现这个代码在JS中,但我不是很擅长JS,我在打印或测试它时遇到了麻烦...有人能检查一下代码并帮助我console.log它吗?
这是我的目标:
┌ ┐ ┌ ┐
|1 2 3| |4 1 2|
|4 5 6| -> |7 5 3|
|7 8 9| |8 9 6|
└ ┘ └ ┘
这是我写的代码:
var matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
function rotateMatrix(matrix) {
//boundery indication for row and column (index)
let maxRow = matrix.length - 1;
let maxCol = matrix.length - 1;
let row = 0;
let col = 0;
//keeps track of ring which we are working with
while (row < maxRow && col < maxCol) {
// initial temp starting with 4 being the numb. moving clockwise
let previous = matrix[row + 1][col];
//loop through TOP ROW (row 0 ) moving 1 spot
for (let i = col; i <= maxCol; i++) {
let current = matrix[row][i]; //current makes space for previous starting w/1
matrix[row][i] = previous; // putting final num in new spot starting w/4 putting in slot 1
previous = current; //leaving the loop with previous = 3
}
row++; //moves to row index 1
//starting in row index 1 loop through RIGHT most COLUMN (col 2) moving 1 spot down
for (let j = row; j <= maxRow; j++) {
let current = matrix[j][maxCol]; //current makes space for previous starting w/6
matrix[j][maxCol] = previous; //starting with 3 moving in slot 6
previous = current; //leaving the loop with previous = 9
}
maxCol--; //changes maxCol to index 1
//loop through the BOTTOM ROW (row 2) moving 1 spot left
for (let j = maxCol; j >= col; j--) {
//starting in col index 1 decrementing down to col 0
let current = matrix[maxRow][j]; //current makes space for previous starting w/8
matrix[maxRow][j] = previous; //starting with 9 moving in slot 8
previous = current; //leaving the loop with previous = 7
}
maxRow--; //changes maxRow to index 1
//loop through the LEFT COLUMN (col 0) moving 1 spot up
for (let i = maxRow; i >= row; i--) {
//starting in row index 1 decrementing down to row 0
let current = matrix[i][col]; //current makes space for previous starting w/4
matrix[i][col] = previous; //starting with 7 moving in slot 4
previous = current; //leaving the loop with previous = 4
}
}
}
///////
console.log(matrix.map(x=>x.join()));
rotateMatrix(matrix);
console.log(matrix.map(x=>x.join()));