2.8 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			2.8 KiB
		
	
	
	
	
	
	
	
id, title, challengeType, forumTopicId
| id | title | challengeType | forumTopicId | 
|---|---|---|---|
| 587d7b86367417b2b2512b3c | Use Caution When Reinitializing Variables Inside a Loop | 1 | 301194 | 
Description
console.log() can uncover buggy behavior related to resetting, or failing to reset a variable.
Instructions
m rows and n columns of zeroes. Unfortunately, it's not producing the expected output because the row variable isn't being reinitialized (set back to an empty array) in the outer loop. Fix the code so it returns a correct 3x2 array of zeroes, which looks like [[0, 0], [0, 0], [0, 0]].
Tests
tests:
  - text: Your code should set the <code>matrix</code> variable to an array holding 3 rows of 2 columns of zeroes each.
    testString: assert(JSON.stringify(matrix) == "[[0,0],[0,0],[0,0]]");
  - text: The <code>matrix</code> variable should have 3 rows.
    testString: assert(matrix.length == 3);
  - text: The <code>matrix</code> variable should have 2 columns in each row.
    testString: assert(matrix[0].length == 2 && matrix[1].length === 2 && matrix[2].length === 2);
Challenge Seed
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);
Solution
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);