Skip to content

Commit 93508e5

Browse files
committed
Refactor 1.8 solution to test helper function and improve readability
1 parent b011b22 commit 93508e5

File tree

1 file changed

+57
-36
lines changed

1 file changed

+57
-36
lines changed

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

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,71 @@ const zeroMatrix = nestedArr => {
66
const rowsAndColsWithZeros = checkForZeroIndex(nestedArr);
77
const rowLength = nestedArr[0].length;
88
const columnLength = nestedArr.length;
9-
for (const rowIndex of rowsAndColsWithZeros['rows']) {
10-
for (let x = 0; x < rowLength; x++) {
11-
nestedArr[rowIndex][x] = 0;
9+
for (const row in rowsAndColsWithZeros['rows']) {
10+
for (let col = 0; col < rowLength; col++) {
11+
nestedArr[row][col] = 0;
1212
}
1313
}
14-
for (const colIndex of rowsAndColsWithZeros['columns']) {
15-
for (let x = 0; x < columnLength; x++) {
16-
nestedArr[x][colIndex] = 0;
14+
for (const col in rowsAndColsWithZeros['columns']) {
15+
for (let row = 0; row < columnLength; row++) {
16+
nestedArr[row][col] = 0;
1717
}
1818
}
1919
return nestedArr;
20+
};
2021

21-
// **** Helper functions ****
22-
function checkForZeroIndex(nestArr) {
23-
let zeroIndices = { rows: [], columns: [] };
24-
for (let i = 0; i < nestArr.length; i++) {
25-
for (let x = 0; x < nestArr[i].length; x++) {
26-
if (nestArr[i][x] === 0) {
27-
zeroIndices['rows'].push(i);
28-
zeroIndices['columns'].push(x);
29-
}
22+
// **** Helper function ****
23+
function checkForZeroIndex(nestArr) {
24+
let zeroIndices = { rows: {}, columns: {} };
25+
for (let row = 0; row < nestArr.length; row++) {
26+
for (let col = 0; col < nestArr[row].length; col++) {
27+
if (nestArr[row][col] === 0) {
28+
zeroIndices['rows'][row] = true;
29+
zeroIndices['columns'][col] = true;
3030
}
3131
}
32-
return zeroIndices;
3332
}
34-
};
33+
return zeroIndices;
34+
}
3535

3636
// **** TESTS ****:
37-
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-
);
37+
let actual = zeroMatrix([[]]);
38+
let expected = [[]];
39+
isEqual(actual, expected);
40+
41+
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]);
42+
expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
43+
isEqual(actual, expected);
44+
45+
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]);
46+
expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
47+
isEqual(actual, expected);
48+
49+
actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]);
50+
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
51+
isEqual(actual, expected);
52+
53+
function isEqual(actual, expected) {
54+
console.log(JSON.stringify(actual) === JSON.stringify(expected));
55+
}
56+
57+
// ****HELPER FUNCTION TESTS ****:
58+
actual = checkForZeroIndex([[1, 2, 0]]);
59+
expected = { rows: { 0: true }, columns: { 2: true } };
60+
testCheckForZeroIndex(actual, expected);
61+
62+
actual = checkForZeroIndex([[0]]);
63+
expected = { rows: { 0: true }, columns: { 0: true } };
64+
testCheckForZeroIndex(actual, expected);
65+
66+
actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]);
67+
expected = { rows: { 1: true, 2: true }, columns: { 1: true } };
68+
testCheckForZeroIndex(actual, expected);
69+
70+
function testCheckForZeroIndex(actual, expected) {
71+
let rows = JSON.stringify(actual.rows);
72+
let cols = JSON.stringify(actual.columns);
73+
let expRows = JSON.stringify(expected.rows);
74+
let expCols = JSON.stringify(expected.columns);
75+
console.log(rows === expRows && cols === expCols);
76+
}

0 commit comments

Comments
 (0)