Skip to content

Commit b011b22

Browse files
committed
Refactor 1.8 Zero Matrix solution for readability
1 parent df78aa5 commit b011b22

File tree

1 file changed

+26
-55
lines changed

1 file changed

+26
-55
lines changed

JavaScript/chapter01/1.8 - Zero Matrix/rroque98_sol.js

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
in an M*N matrix is 0,it's entire row and column
33
are set to 0*/
44

5-
const zeroMatrix = (nestedArr) => {
6-
var rowsAndColsWithZeros = checkForZeroIndex(nestedArr);
7-
let rowLength = nestedArr[0].length;
8-
let columnLength = nestedArr.length;
9-
for (let i = 0; i < rowsAndColsWithZeros['rows'].length; i++) {
10-
let rowIndex = rowsAndColsWithZeros['rows'][i];
5+
const zeroMatrix = nestedArr => {
6+
const rowsAndColsWithZeros = checkForZeroIndex(nestedArr);
7+
const rowLength = nestedArr[0].length;
8+
const columnLength = nestedArr.length;
9+
for (const rowIndex of rowsAndColsWithZeros['rows']) {
1110
for (let x = 0; x < rowLength; x++) {
1211
nestedArr[rowIndex][x] = 0;
1312
}
1413
}
15-
for (let i = 0; i < rowsAndColsWithZeros['columns'].length; i++) {
16-
let colIndex = rowsAndColsWithZeros['columns'][i];
14+
for (const colIndex of rowsAndColsWithZeros['columns']) {
1715
for (let x = 0; x < columnLength; x++) {
1816
nestedArr[x][colIndex] = 0;
1917
}
@@ -22,7 +20,7 @@ const zeroMatrix = (nestedArr) => {
2220

2321
// **** Helper functions ****
2422
function checkForZeroIndex(nestArr) {
25-
let zeroIndices = {rows: [], columns: []};
23+
let zeroIndices = { rows: [], columns: [] };
2624
for (let i = 0; i < nestArr.length; i++) {
2725
for (let x = 0; x < nestArr[i].length; x++) {
2826
if (nestArr[i][x] === 0) {
@@ -33,52 +31,25 @@ const zeroMatrix = (nestedArr) => {
3331
}
3432
return zeroIndices;
3533
}
36-
}
34+
};
3735

3836
// **** TESTS ****:
39-
40-
var testArr1 = [
41-
[3, 5, 6],
42-
[1, 0, 2],
43-
[4, 4, 5],
44-
[2, 2, 2],
45-
]
46-
47-
var resultArr1 = [
48-
[3, 0, 6],
49-
[0, 0, 0],
50-
[4, 0, 5],
51-
[2, 0, 2],
52-
]
53-
54-
var testArr2 = [
55-
[3, 5, 6],
56-
[1, 0, 2],
57-
[4, 4, 0],
58-
[2, 0, 2],
59-
]
60-
61-
var resultArr2 = [
62-
[3, 0, 0],
63-
[0, 0, 0],
64-
[0, 0, 0],
65-
[0, 0, 0],
66-
]
67-
68-
var testArr3 = [
69-
[3, 5, 6],
70-
[0, 0, 2],
71-
[4, 4, 0],
72-
[2, 0, 2],
73-
]
74-
75-
var resultArr3 = [
76-
[0, 0, 0],
77-
[0, 0, 0],
78-
[0, 0, 0],
79-
[0, 0, 0],
80-
]
81-
console.log(JSON.stringify(zeroMatrix(testArr1)) === JSON.stringify(resultArr1));
82-
console.log(JSON.stringify(zeroMatrix(testArr2)) === JSON.stringify(resultArr2));
83-
console.log(JSON.stringify(zeroMatrix(testArr3)) === JSON.stringify(resultArr3));
8437
console.log(JSON.stringify(zeroMatrix([[]])) === JSON.stringify([[]]));
38+
39+
const testArr1 = [[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]];
40+
const resultArr1 = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
41+
console.log(
42+
JSON.stringify(zeroMatrix(testArr1)) === JSON.stringify(resultArr1)
43+
);
44+
45+
const testArr2 = [[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]];
46+
const resultArr2 = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
47+
console.log(
48+
JSON.stringify(zeroMatrix(testArr2)) === JSON.stringify(resultArr2)
49+
);
50+
51+
const testArr3 = [[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]];
52+
const resultArr3 = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
53+
console.log(
54+
JSON.stringify(zeroMatrix(testArr3)) === JSON.stringify(resultArr3)
55+
);

0 commit comments

Comments
 (0)