Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
Can't loop through 2D array using 'rows' in JS or TS

Suppose, I want to create a 2D array of the following structure:

[0, 1, 1, 1]
[1, 0, 0, 0]
[1, 0, 0, 0]

For achieving my goal, at first, I created a 2D array with initial values of 0:

function createGrid(m: number, n: number): number {
    let grid: number[][] = new Array(m).fill(0).map(() => new Array(n).fill(0));

    return 0;
};

Then, I changed the values of the first row (except grid[0][0]) to 1:

function uniquePaths(m: number, n: number): number {
    let grid: number[][] = new Array(m).fill(0).map(() => new Array(n).fill(0));

    for (let i = 1; i <= m; i++) {
        grid[0][i] = 1;
    }

    return 0;
};

Similarly, I tried to change the values of the first column (except grid[0][0]) to 1:

function uniquePaths(m: number, n: number): number {
    let grid: number[][] = new Array(m).fill(0).map(() => new Array(n).fill(0));

    for (let i = 1; i <= m; i++) {
        grid[0][i] = 1;
    }

    for (let i = 1; i <= n; i++) {
        grid[i][0] = 1;   // <<-- 'Throws error here`
    }

    return 0;
};

But, it throws the following error:

grid[i] is undefined

Can someone please explain what I'm missing here?

TypeScript Playground Code Link

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

There is two errors:

  • You swapped m and n when you wanted to fill with 1.
  • You created overflow with condition i <=, you needed i <.

Corrected version:

function uniquePaths(m: number, n: number): number {
    let grid: number[][] = new Array(m).fill(0).map(() => new Array(n).fill(0));

    for (let i = 1; i < n; i++) {
        grid[0][i] = 1;
    }

    for (let i = 1; i < m; i++) {
        grid[i][0] = 1;
    }

    console.log(grid);

    return 0;
};

uniquePaths(3, 4);

Result:

[[0, 1, 1, 1],
 [1, 0, 0, 0],
 [1, 0, 0, 0]] 
about 4 years ago · Juan Pablo Isaza Denunciar

0

You could check the indices and check the logical not value and return a number of the check.

function createGrid(m, n) {
    return Array.from(
        { length: m },
        (_, i) => Array.from(
            { length: n },
            (_, j) => +(!i !== !j)
        )
    );
};

createGrid(3, 4).map(a => console.log(...a))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda