@@ -6,50 +6,71 @@ const zeroMatrix = nestedArr => {
6
6
const rowsAndColsWithZeros = checkForZeroIndex ( nestedArr ) ;
7
7
const rowLength = nestedArr [ 0 ] . length ;
8
8
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 ;
12
12
}
13
13
}
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 ;
17
17
}
18
18
}
19
19
return nestedArr ;
20
+ } ;
20
21
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 ;
30
30
}
31
31
}
32
- return zeroIndices ;
33
32
}
34
- } ;
33
+ return zeroIndices ;
34
+ }
35
35
36
36
// **** 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