Skip to content

Commit 137cc6f

Browse files
authored
Merge pull request #1 from serge-svg/develop
Bonus 8 - Iteration ironhack-labs#8 done!
2 parents aa7ac24 + f3ef8fa commit 137cc6f

File tree

3 files changed

+127
-21
lines changed

3 files changed

+127
-21
lines changed

SpecRunner.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
<script src="tests/functions-and-arrays.spec.js"></script>
1919
</head>
2020

21-
<body></body>
21+
<body>
22+
</body>
2223
</html>

src/functions-and-arrays.js

Lines changed: 113 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,73 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(number1, number2) {
3+
return number1 > number2 ? number1 : number2;
4+
}
55

66
// Iteration #2: Find longest word
77
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
88

9-
function findLongestWord() {}
10-
9+
function findLongestWord(words) {
10+
let longestWord = "";
11+
for(word of words) {
12+
if (word.length > longestWord.length) longestWord = word;
13+
}
1114

15+
return longestWord === "" ? null : longestWord;
16+
}
1217

1318
// Iteration #3: Calculate the sum
1419
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
1520

16-
function sumNumbers() {}
21+
function sumNumbers(numbers) {
22+
let result = 0;
23+
numbers.forEach(element => result += element);
1724

25+
return result;
26+
}
1827

28+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
1929

2030
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
23-
31+
function sum(mixedArr) {
32+
let result = 0;
33+
mixedArr.forEach(element => {
34+
if (typeof(element) === "number") result += element;
35+
if (typeof(element) === "boolean") result += element;
36+
if (typeof(element) === "string") result += element.length;
37+
if (typeof(element) === "object") throw new Error("Unsupported data type sir or ma'am");
38+
});
39+
40+
return result;
41+
}
2442

2543
// Iteration #4: Calculate the average
2644
// Level 1: Array of numbers
2745
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2846

29-
function averageNumbers() {}
30-
47+
function averageNumbers(numbersAvg) {
48+
let sizeOfTheArrayToCompute = numbersAvg.length;
49+
if (!(sizeOfTheArrayToCompute > 0)) return null;
50+
51+
return sumNumbers(numbersAvg) / sizeOfTheArrayToCompute;;
52+
}
3153

3254
// Level 2: Array of strings
3355
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
3456

35-
function averageWordLength() { }
57+
function averageWordLength(wordsArr) {
58+
let sizeOfTheArrayToCompute = wordsArr.length;
59+
if (!(sizeOfTheArrayToCompute > 0)) return null;
60+
61+
return sum(wordsArr) / sizeOfTheArrayToCompute;
62+
}
3663

3764
// Bonus - Iteration #4.1
38-
function avg() {}
65+
function avg(mixedArr) {
66+
let sizeOfTheArrayToCompute = mixedArr.length;
67+
if (!(sizeOfTheArrayToCompute > 0)) return null;
68+
69+
return sum(mixedArr) / sizeOfTheArrayToCompute;
70+
}
3971

4072
// Iteration #5: Unique arrays
4173
const wordsUnique = [
@@ -52,16 +84,26 @@ const wordsUnique = [
5284
'bring'
5385
];
5486

55-
function uniquifyArray() {}
56-
87+
function uniquifyArray(wordsUnique) {
88+
let sizeOfTheArrayToCompute = wordsUnique.length;
89+
if (!(sizeOfTheArrayToCompute > 0)) return null;
90+
let uniqueArray = [];
91+
for(word of wordsUnique){
92+
if(!(uniqueArray.includes(word))) uniqueArray.push(word);
93+
}
5794

95+
return uniqueArray;
96+
}
5897

5998
// Iteration #6: Find elements
6099
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61100

62-
function doesWordExist() {}
63-
101+
function doesWordExist(wordsFind, wordToFind) {
102+
let sizeOfTheArrayToCompute = wordsFind.length;
103+
if (!(sizeOfTheArrayToCompute > 0)) return null;
64104

105+
return wordsFind.indexOf(wordToFind) === -1 ? false : true;
106+
}
65107

66108
// Iteration #7: Count repetition
67109
const wordsCount = [
@@ -78,8 +120,15 @@ const wordsCount = [
78120
'matter'
79121
];
80122

81-
function howManyTimes() {}
123+
function howManyTimes(wordsCount, wordToCount) {
124+
if (wordsCount === undefined) return 0;
125+
let counter = 0;
126+
for (word of wordsCount) {
127+
if (word === wordToCount) counter++
128+
}
82129

130+
return counter;
131+
}
83132

84133

85134
// Iteration #8: Bonus
@@ -106,7 +155,52 @@ const matrix = [
106155
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107156
];
108157

109-
function greatestProduct() {}
158+
function greatestProduct(matrix) {
159+
let greatestProduct = 0;
160+
161+
for(let row = 0; row < matrix.length - 1; row++){
162+
for(let column = 0; column < matrix[row].length - 1; column++){
163+
let multiplicationResult = 0;
164+
try {
165+
if(matrix[row][column + 4] !== undefined) {
166+
multiplicationResult = matrix[row][column + 1]
167+
* matrix[row][column + 2]
168+
* matrix[row][column + 3]
169+
* matrix[row][column + 4];
170+
greatestProduct = (multiplicationResult > greatestProduct) ? multiplicationResult : greatestProduct;
171+
}
172+
173+
if(matrix[row + 4][column] !== undefined) {
174+
multiplicationResult = matrix[row + 1][column]
175+
* matrix[row + 2][column]
176+
* matrix[row + 3][column]
177+
* matrix[row + 4][column];
178+
greatestProduct = (multiplicationResult > greatestProduct) ? multiplicationResult : greatestProduct;
179+
}
180+
} catch (error){
181+
/* to control the undefied */
182+
}
183+
}
184+
}
185+
186+
return greatestProduct;
187+
}
188+
189+
190+
191+
192+
193+
194+
195+
196+
197+
198+
199+
200+
201+
202+
203+
110204

111205

112206

tests/functions-and-arrays.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,19 @@ describe('Bonus Quest - greatestProduct', () => {
277277
expect(typeof greatestProduct).toBe('function');
278278
});
279279

280+
it('should return (32000)', () => {
281+
let matrix = [
282+
[ 1, 2, 3, 4, 5],
283+
[ 1, 20, 3, 4, 5],
284+
[ 1, 20, 3, 4, 5],
285+
[ 1, 20, 3, 4, 5],
286+
[ 1, 4, 3, 4, 5]
287+
];
288+
expect(greatestProduct(matrix)).toBe(32000);
289+
});
290+
280291
it('should return 1 (one) when all numbers of the arrays are 1', () => {
281-
let matrix = [
292+
let matrix = [
282293
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
283294
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
284295
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],

0 commit comments

Comments
 (0)