Skip to content

Commit 46c711b

Browse files
solved lab
1 parent 9920663 commit 46c711b

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

src/functions-and-arrays.js

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ function averageWordLength(words) {
7272
// Bonus - Iteration #4.1
7373
function avg(arr) {
7474
if (!arr.length) return null;
75-
const validTypes=['number','string','boolean']
75+
const validTypes = [ 'number', 'string', 'boolean' ]
7676

7777
return arr.reduce((acc, value) => {
7878
if (!validTypes.includes(typeof value)) {
7979
throw new Error("Unsupported data type");
8080
}
81-
81+
8282
if (typeof value === 'number') return acc + value;
8383
if (typeof value === 'string') return acc + value.length;
8484
if (typeof value === 'boolean') return acc + (value ? 1 : 0);
85-
85+
8686
}, 0) / arr.length;
8787
}
8888

@@ -102,7 +102,7 @@ const wordsUnique = [
102102
'bring'
103103
];
104104

105-
function uniquifyArray(arr) {
105+
function uniquifyArray(arr) {
106106
if (!arr.length) return null;
107107
return Array.from(new Set(arr))
108108
}
@@ -112,7 +112,7 @@ function uniquifyArray(arr) {
112112
// Iteration #6: Find elements
113113
const wordsFind = [ 'machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience' ];
114114

115-
function doesWordExist(arr, word) {
115+
function doesWordExist(arr, word) {
116116
if (!arr.length) return null;
117117
return arr.includes(word);
118118
}
@@ -134,8 +134,8 @@ const wordsCount = [
134134
'matter'
135135
];
136136

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
139139
}
140140

141141

@@ -165,9 +165,69 @@ const matrix = [
165165
];
166166

167167
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+
168227

169-
170-
}
228+
229+
return maxProduct;
230+
}
171231

172232

173233

0 commit comments

Comments
 (0)