-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-use-caution-when-reinitializing-variables-inside-a-loop.js
51 lines (46 loc) · 1.73 KB
/
11-use-caution-when-reinitializing-variables-inside-a-loop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Use Caution When Reinitializing Variables Inside a Loop:
The following function is supposed to create a two-dimensional array with 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]].
- Your code should set the matrix variable to an array holding 3 rows of 2 columns of zeroes each.
- The matrix variable should have 3 rows.
- The matrix variable should have 2 columns in each row.
- zeroArray(4,3) should return an array holding 4 rows of 3 columns of zeroes each.
Initial code:
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);
*/
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);