freeCodeCamp/curriculum/challenges/espanol/02-javascript-algorithms-an.../debugging/use-caution-when-reinitiali...

2.7 KiB

id title challengeType forumTopicId dashedName
587d7b86367417b2b2512b3c Ten cuidado al reinicializar variables dentro de un bucle 1 301194 use-caution-when-reinitializing-variables-inside-a-loop

--description--

A veces es necesario guardar información, incrementar contadores o reajustar variables dentro de un bucle. Un problema potencial es cuando las variables deberían ser reiniciadas y no lo son, o viceversa. Esto es particularmente peligroso si accidentalmente se restablece la variable que se utiliza para la condición terminal, causando un bucle infinito.

La impresión de los valores de las variables con cada ciclo de su bucle mediante el uso de console.log() puede descubrir un comportamiento erróneo relacionado con el restablecimiento, o la falta de restablecimiento de una variable.

--instructions--

La siguiente función debe crear un arreglo bidimensional (matriz) con m filas (rows) y n columnas (columns) de ceros. Desafortunadamente, no está produciendo la salida esperada porque la variable row no está siendo reiniciada (devuelta a un arreglo vacío) en el bucle exterior. Corrige el código para que devuelva una matriz 3x2 de ceros correcta, que se parezca a [[0, 0], [0, 0], [0, 0]].

--hints--

Tu código debe establecer la variable matrix en una matriz que contenga 3 filas de 2 columnas de ceros cada una.

assert(JSON.stringify(matrix) == '[[0,0],[0,0],[0,0]]');

La variable matrix debe tener 3 filas.

assert(matrix.length == 3);

La variable matrix debe tener 2 columnas en cada fila.

assert(
  matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2
);

--seed--

--seed-contents--

function zeroArray(m, n) {
  // Creates a 2-D array with m rows and n columns of zeroes
  let newArray = [];
  let row = [];
  for (let i = 0; i < m; i++) {
    // Adds the m-th row into newArray

    for (let j = 0; j < n; j++) {
      // Pushes n zeroes into the current row to create the columns
      row.push(0);
    }
    // Pushes the current row, which now has n zeroes in it, to the array
    newArray.push(row);
  }
  return newArray;
}

let matrix = zeroArray(3, 2);
console.log(matrix);

--solutions--

function zeroArray(m, n) {
 // Creates a 2-D array with m rows and n columns of zeroes
 let newArray = [];
 for (let i = 0; i < m; i++) {
   let row = [];
   // Adds the m-th row into newArray

   for (let j = 0; j < n; j++) {
     // Pushes n zeroes into the current row to create the columns
     row.push(0);
   }
   // Pushes the current row, which now has n zeroes in it, to the array
   newArray.push(row);
 }
 return newArray;
}

let matrix = zeroArray(3, 2);
console.log(matrix);