SOURCE

// 1 2 3
// 4 5 6
// 7 8 9

function fn(matrix) {
    const len = matrix.length;
    if(len<=1) return len===1? matrix: [[]];

    const outerLoop = Math.floor(len/2);
    for(let i=0;i<=outerLoop;i++) {
        for(let j=i;j<(len-i-1);j++) {
            // i,i+j => i+j,len-1-i => len-1-i,len-1-i-j => len-1-i-j,i
            const first = matrix[i][i+j];
            const sec = matrix[i+j][len-1-i];
            const third = matrix[len-1-i][len-1-i-j];
            const forth = matrix[len-1-i-j][i];
            console.log(first, sec, third, forth)
            matrix[i+j][len-1-i] = first;
            matrix[len-1-i][len-1-i-j] = sec;
            matrix[len-1-i-j][i] = third;
            matrix[i][i+j] = forth;
        }
    }
    
    return matrix;
}

const matrix = [[1,2,3],[4,5,6],[7,8,9]]//[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
console.log(fn(matrix));
// fn(matrix)
console 命令行工具 X clear

                    
>
console