Skip to content

Commit 44572c3

Browse files
author
Pedro Louro
committed
Add Bonus ironhack-labs#8.1 to the lab solvd
1 parent 414110e commit 44572c3

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/functions-and-arrays.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function sum(arrayOfEverything) {
4646
let total = 0;
4747
for (let element of arrayOfEverything) {
4848
if (typeof(element) === 'array' || typeof(element) === 'object') {
49-
throw new Error('invalid input')
49+
throw new Error(`Unsupported data type sir or ma'am`);
5050
}
5151
else {
5252
if (typeof(element) === 'string') {
@@ -229,9 +229,49 @@ const matrix = [
229229
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
230230
];
231231

232-
function greatestProduct() {}
232+
function greatestProduct(matrix) {
233+
let productHorizontal = 0;
234+
let productVertical = 0;
235+
let maxResult = 0;
236+
237+
for (let element = 0; element < matrix.length; element ++) {
238+
for (let i = 0; i < matrix[element].length; i++) {
239+
// Horizontal
240+
if (i + 1 >= matrix[element].length || i + 2 >= matrix[element].length || i + 3 >= matrix[element].length) {
241+
console.log('Part horizontal iF statment');
242+
}
243+
else {
244+
productHorizontal = matrix[element][i] * matrix[element][i + 1] * matrix[element][i + 2] * matrix[element][i + 3];
245+
console.log(`Horizontal: ${matrix[element][i]} * ${matrix[element][i + 1]} * ${matrix[element][i + 2]} * ${matrix[element][i + 3]}`);
246+
}
247+
248+
//Vertical
249+
if (element +1 >= matrix.length || element +2 >= matrix.length || element +3 >= matrix.length) {
250+
console.log('Part 1 of iF statment');
251+
}
252+
else {
253+
productVertical = matrix[element][i] * matrix[element +1][i] * matrix[element +2][i] * matrix[element +3][i];
254+
console.log(`Vertical: element: ${matrix[element][i]} * ${matrix[element +1][i]} * ${matrix[element +2][i]} * ${matrix[element +3][i]}`);
255+
}
233256

257+
//check if any of the products is higher than the last maxResult
258+
if (productHorizontal > maxResult || productVertical > maxResult) {
259+
if (productHorizontal > productVertical) {
260+
maxResult = productHorizontal;
261+
}
262+
else {
263+
maxResult = productVertical;
264+
};
265+
}
266+
//Reset productHorizontal and productVertical
267+
productHorizontal = 0;
268+
productVertical = 0;
269+
}
270+
}
271+
return maxResult;
272+
}
234273

274+
console.log(greatestProduct(matrix))
235275

236276

237277
// The following is required to make unit tests work.

0 commit comments

Comments
 (0)