@@ -72,17 +72,17 @@ function averageWordLength(words) {
72
72
// Bonus - Iteration #4.1
73
73
function avg ( arr ) {
74
74
if ( ! arr . length ) return null ;
75
- const validTypes = [ 'number' , 'string' , 'boolean' ]
75
+ const validTypes = [ 'number' , 'string' , 'boolean' ]
76
76
77
77
return arr . reduce ( ( acc , value ) => {
78
78
if ( ! validTypes . includes ( typeof value ) ) {
79
79
throw new Error ( "Unsupported data type" ) ;
80
80
}
81
-
81
+
82
82
if ( typeof value === 'number' ) return acc + value ;
83
83
if ( typeof value === 'string' ) return acc + value . length ;
84
84
if ( typeof value === 'boolean' ) return acc + ( value ? 1 : 0 ) ;
85
-
85
+
86
86
} , 0 ) / arr . length ;
87
87
}
88
88
@@ -102,7 +102,7 @@ const wordsUnique = [
102
102
'bring'
103
103
] ;
104
104
105
- function uniquifyArray ( arr ) {
105
+ function uniquifyArray ( arr ) {
106
106
if ( ! arr . length ) return null ;
107
107
return Array . from ( new Set ( arr ) )
108
108
}
@@ -112,7 +112,7 @@ function uniquifyArray(arr) {
112
112
// Iteration #6: Find elements
113
113
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
114
114
115
- function doesWordExist ( arr , word ) {
115
+ function doesWordExist ( arr , word ) {
116
116
if ( ! arr . length ) return null ;
117
117
return arr . includes ( word ) ;
118
118
}
@@ -134,8 +134,8 @@ const wordsCount = [
134
134
'matter'
135
135
] ;
136
136
137
- function howManyTimes ( arr , word ) {
138
- return arr . filter ( el => el === word ) . length
137
+ function howManyTimes ( arr , word ) {
138
+ return arr . filter ( el => el === word ) . length
139
139
}
140
140
141
141
@@ -165,9 +165,69 @@ const matrix = [
165
165
] ;
166
166
167
167
function greatestProduct ( matrix ) {
168
+ let maxProduct = 0 ;
169
+ const rows = matrix . length ;
170
+ const cols = matrix [ 0 ] . length ;
171
+
172
+ // horizontal
173
+ for ( let row = 0 ; row < rows ; row ++ ) {
174
+ for ( let col = 0 ; col < cols ; col ++ ) {
175
+ let product = 1 ;
176
+ for ( let limit = 0 ; limit < 4 ; limit ++ ) {
177
+ product *= matrix [ row ] [ col + limit ]
178
+ }
179
+ if ( product > maxProduct ) {
180
+ maxProduct = product ;
181
+
182
+ }
183
+ }
184
+ }
185
+ // vertical
186
+ for ( let row = 0 ; row < rows - 4 ; row ++ ) {
187
+ for ( let col = 0 ; col < cols ; col ++ ) {
188
+ let product = 1 ;
189
+ for ( let limit = 0 ; limit < 4 ; limit ++ ) {
190
+ product *= matrix [ row + limit ] [ col ]
191
+ }
192
+ if ( product > maxProduct ) {
193
+ maxProduct = product ;
194
+
195
+ }
196
+ }
197
+ // diagonals
198
+ for ( let row = 0 ; row < rows - 4 ; row ++ ) {
199
+ // empezamos en col=3 para no salirnos por la izquierda
200
+ for ( let col = 3 ; col < cols ; col ++ ) {
201
+ // ↘ diagonales hacia la derecha
202
+ if ( cols + 3 < cols ) {
203
+ let product = 1 ;
204
+ for ( let i = 0 ; i < 4 ; i ++ ) {
205
+ product *= matrix [ row + i ] [ col - i ] ; // ↙
206
+ }
207
+ if ( product > maxProduct ) {
208
+ maxProduct = product ;
209
+ }
210
+
211
+ }
212
+
213
+ if ( cols - 3 >= 0 ) {
214
+ let product = 1 ;
215
+ for ( let i = 0 ; i < 4 ; i ++ ) {
216
+ product *= matrix [ row + i ] [ col - i ] ;
217
+ }
218
+ if ( product > maxProduct ) {
219
+ maxProduct = product ;
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+ // ↙ diagonales hacia la izquierda
226
+
168
227
169
-
170
- }
228
+
229
+ return maxProduct ;
230
+ }
171
231
172
232
173
233
0 commit comments