Skip to content

Commit dc99953

Browse files
committed
Bonus - Iteration ironhack-labs#8: Product of adjacent numbers
1 parent bbd5108 commit dc99953

File tree

2 files changed

+196
-43
lines changed

2 files changed

+196
-43
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"prettier": {
3434
"printWidth": 120,
3535
"singleQuote": true,
36-
"trailingComma": "none"
36+
"trailingComma": "none",
37+
"editor.formatOnSave": true,
38+
"semi": false
3739
}
3840
}

src/functions-and-arrays.js

Lines changed: 193 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,110 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(number1, number2) {
3+
if (number1 >= number2) {
4+
return number1
5+
} else {
6+
return number2
7+
}
8+
}
59

610
// Iteration #2: Find longest word
7-
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
8-
9-
function findLongestWord() {}
10-
11-
11+
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot']
12+
13+
function findLongestWord(wordsArray) {
14+
if (wordsArray.length == 0) {
15+
return null
16+
}
17+
if (wordsArray.length == 1) {
18+
return wordsArray[0]
19+
}
20+
let longestWord = ''
21+
let longestLength = 0
22+
for (let i = 0; i < wordsArray.length; i++) {
23+
if (wordsArray[i].length > longestLength) {
24+
longestLength = wordsArray[i].length
25+
longestWord = wordsArray[i]
26+
}
27+
}
28+
return longestWord
29+
}
1230

1331
// Iteration #3: Calculate the sum
14-
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
15-
16-
function sumNumbers() {}
17-
18-
32+
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10]
33+
34+
function sumNumbers(numbersArray) {
35+
if (numbersArray.length == 0) {
36+
return 0
37+
}
38+
if (numbersArray.length == 1) {
39+
return numbersArray[0]
40+
}
41+
let partialSum = 0
42+
for (let i = 0; i < numbersArray.length; i++) {
43+
partialSum += numbersArray[i]
44+
}
45+
return partialSum
46+
}
1947

2048
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
23-
49+
function sum(genericArray) {
50+
if (genericArray.length == 0) {
51+
return 0
52+
}
53+
if (genericArray.length == 1) {
54+
return genericArray[0]
55+
}
56+
let partialSum = 0
57+
for (let i = 0; i < genericArray.length; i++) {
58+
if (typeof genericArray[i] === 'string') {
59+
partialSum += genericArray[i].length
60+
} else if (typeof genericArray[i] === 'boolean') {
61+
if (genericArray[i] === true) {
62+
partialSum++
63+
}
64+
} else if (typeof genericArray[i] === 'object' || typeof genericArray[i] === 'array') {
65+
throw "Unsupported data type sir or ma'am"
66+
} else {
67+
partialSum += genericArray[i]
68+
}
69+
}
70+
return partialSum
71+
}
2472

2573
// Iteration #4: Calculate the average
2674
// Level 1: Array of numbers
27-
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
28-
29-
function averageNumbers() {}
30-
75+
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9]
76+
77+
function averageNumbers(numbersArray) {
78+
if (numbersArray.length == 0) {
79+
return null
80+
}
81+
if (numbersArray.length == 1) {
82+
return numbersArray[0]
83+
}
84+
return sumNumbers(numbersArray) / numbersArray.length
85+
}
3186

3287
// Level 2: Array of strings
33-
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
34-
35-
function averageWordLength() { }
88+
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace']
89+
90+
function averageWordLength(wordsArray) {
91+
if (wordsArray.length == 0) {
92+
return null
93+
}
94+
let partialSum = 0
95+
for (let i = 0; i < wordsArray.length; i++) {
96+
partialSum += wordsArray[i].length
97+
}
98+
return partialSum / wordsArray.length
99+
}
36100

37101
// Bonus - Iteration #4.1
38-
function avg() {}
102+
function avg(mixedArray) {
103+
if (mixedArray.length == 0) {
104+
return null
105+
}
106+
return Math.round((sum(mixedArray) / mixedArray.length) * 100) / 100
107+
}
39108

40109
// Iteration #5: Unique arrays
41110
const wordsUnique = [
@@ -50,18 +119,37 @@ const wordsUnique = [
50119
'communion',
51120
'simple',
52121
'bring'
53-
];
54-
55-
function uniquifyArray() {}
56-
57-
122+
]
123+
124+
function uniquifyArray(arrayToBeUniquified) {
125+
if (arrayToBeUniquified.length == 0) {
126+
return null
127+
}
128+
let uniquifiedArray = []
129+
for (let i = 0; i < arrayToBeUniquified.length; i++) {
130+
if (uniquifiedArray.includes(arrayToBeUniquified[i])) {
131+
continue
132+
} else {
133+
uniquifiedArray.push(arrayToBeUniquified[i])
134+
}
135+
}
136+
return uniquifiedArray
137+
}
58138

59139
// Iteration #6: Find elements
60-
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61-
62-
function doesWordExist() {}
63-
64-
140+
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience']
141+
142+
function doesWordExist(wordsArray, wordToSearch) {
143+
if (wordsArray.length == 0) {
144+
return null
145+
}
146+
for (let i = 0; i < wordsArray.length; i++) {
147+
if (wordsArray[i] === wordToSearch) {
148+
return true
149+
}
150+
}
151+
return false
152+
}
65153

66154
// Iteration #7: Count repetition
67155
const wordsCount = [
@@ -76,11 +164,20 @@ const wordsCount = [
76164
'truth',
77165
'disobedience',
78166
'matter'
79-
];
80-
81-
function howManyTimes() {}
82-
83-
167+
]
168+
169+
function howManyTimes(wordsArray, wordToSearch) {
170+
if (wordsArray.length == 0) {
171+
return 0
172+
}
173+
let countTimes = 0
174+
for (let i = 0; i < wordsArray.length; i++) {
175+
if (wordsArray[i] === wordToSearch) {
176+
countTimes++
177+
}
178+
}
179+
return countTimes
180+
}
84181

85182
// Iteration #8: Bonus
86183
const matrix = [
@@ -104,12 +201,66 @@ const matrix = [
104201
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
105202
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
106203
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107-
];
204+
]
108205

109-
function greatestProduct() {}
206+
function greatestLineProduct(arrayToTest) {
207+
let greatestPartialLineProduct = 0
208+
let partialLineProduct = 0
209+
for (let i = 0; i < arrayToTest.length - 3; i++) {
210+
partialLineProduct = arrayToTest[i] * arrayToTest[i + 1] * arrayToTest[i + 2] * arrayToTest[i + 3]
110211

212+
if (partialLineProduct > greatestPartialLineProduct) {
213+
greatestPartialLineProduct = partialLineProduct
214+
}
215+
}
216+
217+
return greatestPartialLineProduct
218+
}
111219

220+
function transposeMatrix(matrixToTranspose) {
221+
const transposedMatrix = []
222+
223+
for (let i = 0; i < matrixToTranspose[0].length; i++) {
224+
transposedMatrix.push([])
225+
for (let j = 0; j < matrixToTranspose.length; j++) {
226+
transposedMatrix[i].push(matrixToTranspose[j][i])
227+
}
228+
}
229+
return transposedMatrix
230+
}
231+
232+
const tm = [
233+
[1, 2, 3, 4, 5],
234+
[1, 20, 3, 4, 5],
235+
[1, 20, 3, 4, 5],
236+
[1, 20, 3, 4, 5],
237+
[1, 4, 3, 4, 5]
238+
]
239+
240+
function greatestProduct(matrixToTest) {
241+
let greatestGreatestLineProduct = 0
242+
for (let i = 0; i < matrixToTest.length; i++) {
243+
if (greatestLineProduct(matrixToTest[i]) > greatestGreatestLineProduct) {
244+
greatestGreatestLineProduct = greatestLineProduct(matrixToTest[i])
245+
}
246+
}
247+
console.log('greatestGreatestLineProduct', greatestGreatestLineProduct)
248+
let greatestGreatestColumnProduct = 0
249+
const transposedMatrix = transposeMatrix(matrixToTest)
250+
for (let j = 0; j < transposedMatrix.length; j++) {
251+
if (greatestLineProduct(transposedMatrix[j]) > greatestGreatestColumnProduct) {
252+
greatestGreatestColumnProduct = greatestLineProduct(transposedMatrix[j])
253+
}
254+
}
255+
console.log('greatestGreatestColumnProduct', greatestGreatestColumnProduct)
256+
if (greatestGreatestLineProduct >= greatestGreatestColumnProduct) {
257+
return greatestGreatestLineProduct
258+
} else {
259+
return greatestGreatestColumnProduct
260+
}
261+
}
112262

263+
console.log(greatestProduct(matrix))
113264

114265
// The following is required to make unit tests work.
115266
/* Environment setup. Do not modify the below code. */
@@ -126,5 +277,5 @@ if (typeof module !== 'undefined') {
126277
doesWordExist,
127278
howManyTimes,
128279
greatestProduct
129-
};
280+
}
130281
}

0 commit comments

Comments
 (0)