Skip to content

Commit 295c32b

Browse files
committed
Iteration ironhack-labs#8: Bonus
1 parent d3379dd commit 295c32b

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/functions-and-arrays.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ function uniquifyArray(wordsArr) {
138138
// Iteration #6: Find elements
139139
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
140140

141+
function doesWordExist(wordsArr, wordToSearch) {
142+
if (wordsArr.length<=0) {
143+
return null
144+
}
145+
146+
for (let i = 0; i < wordsArr.length; i++) {
147+
if (wordsArr[i] === wordToSearch) {
148+
return true
149+
}
150+
}
151+
return false
152+
}
153+
141154
// Iteration #7: Count repetition
142155
const wordsCount = [
143156
'machine',
@@ -153,6 +166,17 @@ const wordsCount = [
153166
'matter'
154167
];
155168

169+
function howManyTimes(wordsArr, wordToCount) {
170+
let counter = 0
171+
for (let i = 0; i < wordsArr.length; i++) {
172+
if (wordsArr[i] === wordToCount) {
173+
counter++
174+
}
175+
}
176+
177+
return counter
178+
}
179+
156180
// Iteration #8: Bonus
157181

158182
const matrix = [
@@ -177,3 +201,34 @@ const matrix = [
177201
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
178202
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
179203
];
204+
205+
function greatestProduct(matrix) {
206+
//let currentGreatestProductArr = []
207+
let currentHighestProduct = 0
208+
for (let i = 0; i < matrix.length; i++) {
209+
for (let j = 0; j < matrix[i].length; j++) {
210+
let tempProductHorz = 0
211+
let tempProductVert = 0
212+
if(j+3< matrix[i].length) {
213+
tempProductHorz = matrix[i][j] * matrix[i][j + 1] * matrix[i][j + 2] * matrix[i][j + 3]
214+
}
215+
if(i+3< matrix.length) {
216+
tempProductVert = matrix[i][j] * matrix[i + 1][j] * matrix[i+ 2][j] * matrix[i+ 3][j]
217+
}
218+
if(tempProductHorz > tempProductVert) {
219+
if(currentHighestProduct < tempProductHorz) {
220+
currentHighestProduct = tempProductHorz
221+
//currentGreatestProductArr = [matrix[i][j], matrix[i][j + 1] , matrix[i][j + 2] , matrix[i][j + 3]]
222+
}
223+
}
224+
else {
225+
if(currentHighestProduct < tempProductVert) {
226+
currentHighestProduct = tempProductVert
227+
//currentGreatestProductArr = [ matrix[i][j] , matrix[i + 1][j] , matrix[i+ 2][j] , matrix[i+ 3][j]]
228+
}
229+
}
230+
}
231+
}
232+
console.log(currentHighestProduct)
233+
return currentHighestProduct
234+
}

0 commit comments

Comments
 (0)