Javascript for循环跳过矩阵内部的旋转

Javascript for loop skipping a rotation inside matrix

我已经苦思冥想了一段时间,试图找出为什么我的 for 循环会跳过矩阵中的旋转。我试图让它打印出 [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [4, 5, 6, 7, 8]],这是整个矩阵.

function solve(args)
{
    let arr = args[0].split(' ').map(Number),
        rows = +arr[0],
        cols = +arr[1];

    let matrix = new Array(rows);
    matrix.fill();
    for (let i in matrix)
    {
        matrix[i] = new Array(cols);
    }

    for (let row = 0; row < rows; row++)
    {
        matrix[row][0] = Math.pow(2, row);
        for (let col = matrix[row][0]; col < cols; col++)
        {
            matrix[row][col] = +col + 1;
        }
    }
    console.log(matrix);
}

solve([
  '3 5'  
]);

P.S。我也试过这个:

for (let row = 0; row < rows; row++)
    {
        matrix[row][0] = Math.pow(2, row);
        for (let col = matrix[row][0]; col < cols; col++)
        {
            matrix[row][col] = +col + matrix[row][0];
        }
    }

希望对您有所帮助。当以某个值作为第一个元素 (matrix[row][0]) 开始时,您可以迭代地将每一行递增 1:

function solve(args)
{
  let arr = args[0].split(' '),
  rows = +arr[0],
  cols = +arr[1];
  let matrix = new Array(rows);
  matrix.fill();
  for (let i in matrix)
  {
    matrix[i] = new Array(cols);
  }
  for (let row = 0; row < matrix.length; row++) {
    matrix[row][0] = Math.pow(2, row);
    for (let col = 1; col < matrix[row].length; col++) {
      matrix[row][col] = col + matrix[row][0];
    }
  }
  console.log(matrix);
  return matrix;
}

solve([
'3 5'
]);