Skip to content

Commit 58d7cc6

Browse files
ironhack-eduCruzMauSerrano
authored andcommitted
Merge pull request ironhack-labs#3925 from ironhack-labs/develop
Review changes in iteration 3.2 and update the test assertion
2 parents 751ea3b + c09a8d0 commit 58d7cc6

File tree

3 files changed

+170
-18
lines changed

3 files changed

+170
-18
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
155155

156156
In iteration 3, you created a function that returns the sum of an array of numbers. But what if we want to calculate the sum of the length of words in an array? What if it also includes _boolean_ values? To achieve this, we must create a function allowing this flexibility.
157157

158-
You should implement the function `sum()` in this iteration. The function should take an array of mixed values - numbers, strings, and booleans. The function should add all the string lengths, numeric values, and numeric values of booleans to the total sum and return the sum. Check the tests for more details.
158+
You should implement the function `sum()` in this iteration. The function should take an array of mixed values - numbers, strings, and booleans. The function should add all the string lengths, numeric values, and numeric values of booleans to the total sum and return the sum.
159159

160160
You can use the following array to test your solution:
161161

@@ -165,6 +165,14 @@ const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
165165
// should return: 57
166166
```
167167

168+
169+
Note: Your function should only accept an array with numbers, strings, or booleans. If the array contains any other data type, such as an object, you should [throw an error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw). In JavaScript, the syntax for throwing an error is as follows:
170+
```javascript
171+
throw new Error("Error message goes here");
172+
```
173+
174+
When specifying the error message, you should be specific and descriptive in explaining the error.
175+
168176
<br>
169177

170178
### Iteration #4: Calculate the average
@@ -581,4 +589,4 @@ If the link shown is the same as the main Ironhack repository, you will need to
581589
582590
[Back to top](#faqs)
583591
584-
</details>
592+
</details>

src/functions-and-arrays.js

Lines changed: 159 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,131 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
2+
3+
function maxOfTwoNumbers(num1, num2) {
4+
if (num1 > num2) {
5+
return num1;
6+
} else if (num2 > num1) {
7+
return num2;
8+
} else {
9+
return num1;
10+
}
11+
}
312

413

514

615
// Iteration #2: Find longest word
716
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
817

9-
function findLongestWord() {}
18+
function findLongestWord(words) {
19+
if (words.length === 0) {
20+
return null;
21+
}
22+
let longestWord = words[0];
23+
24+
for (let i = 1; i < words.length; i++) {
25+
if (words[i].length > longestWord.length) {
26+
longestWord = words[i];
27+
}
28+
}
29+
30+
return longestWord;
31+
}
1032

1133

1234

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

16-
function sumNumbers() {}
38+
function sumNumbers(numbers) {
39+
if (numbers.length === 0) {
40+
return 0;
41+
}
42+
43+
let suma = 0;
44+
45+
for (let i = 0; i < numbers.length; i++) {
46+
suma += numbers[i];
47+
}
48+
49+
return suma;
50+
}
1751

1852

1953

2054
// Iteration #3.1 Bonus:
21-
function sum() {}
55+
function sum(arr) {
56+
for (let item of arr) {
57+
if (typeof item === 'object' && item !== null) {
58+
throw new Error('Unsupported data type');
59+
}
60+
}
61+
62+
return arr.reduce((total, item) => {
63+
if (typeof item === 'number') {
64+
return total + item;
65+
} else if (typeof item === 'string') {
66+
return total + item.length;
67+
} else if (typeof item === 'boolean') {
68+
return total + (item ? 1 : 0);
69+
} else {
70+
return total;
71+
}
72+
}, 0);
73+
}
2274

2375

2476

2577
// Iteration #4: Calculate the average
2678
// Level 1: Array of numbers
27-
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
79+
function averageNumbers(arr) {
80+
if (arr.length === 0) {
81+
return null;
82+
}
2883

29-
function averageNumbers() {}
84+
const sum = arr.reduce((total, num) => total + num, 0);
3085

86+
const average = sum / arr.length;
87+
88+
return average;
89+
}
3190

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

35-
function averageWordLength() { }
94+
function averageWordLength(arr) {
95+
96+
if (arr.length === 0) {
97+
return null;
98+
}
99+
100+
const sumOfLengths = arr.reduce((total, word) => total + word.length, 0);
101+
102+
const averageLength = sumOfLengths / arr.length;
103+
104+
return averageLength;
105+
}
36106

37107
// Bonus - Iteration #4.1
38-
function avg() {}
108+
function avg(arr) {
109+
if (arr.length === 0) {
110+
return null;
111+
}
112+
113+
const sum = arr.reduce((total, item) => {
114+
if (typeof item === 'number') {
115+
return total + item;
116+
} else if (typeof item === 'string') {
117+
return total + item.length;
118+
} else if (typeof item === 'boolean') {
119+
return total + (item ? 1 : 0);
120+
} else {
121+
throw new Error('Unsupported data type');
122+
}
123+
}, 0);
124+
125+
const average = sum / arr.length;
126+
127+
return average;
128+
}
39129

40130
// Iteration #5: Unique arrays
41131
const wordsUnique = [
@@ -52,14 +142,28 @@ const wordsUnique = [
52142
'bring'
53143
];
54144

55-
function uniquifyArray() {}
145+
function uniquifyArray(arr) {
146+
if (arr.length === 0) {
147+
return null;
148+
}
149+
150+
const uniqueSet = new Set(arr);
151+
152+
return Array.from(uniqueSet);
153+
}
56154

57155

58156

59157
// Iteration #6: Find elements
60158
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61159

62-
function doesWordExist() {}
160+
function doesWordExist(arr, word) {
161+
if (arr.length === 0) {
162+
return null;
163+
}
164+
165+
return arr.includes(word);
166+
}
63167

64168

65169

@@ -78,7 +182,21 @@ const wordsCount = [
78182
'matter'
79183
];
80184

81-
function howManyTimes() {}
185+
function howManyTimes(arr, word) {
186+
if (arr.length === 0) {
187+
return 0;
188+
}
189+
190+
let count = 0;
191+
192+
for (let i = 0; i < arr.length; i++) {
193+
if (arr[i] === word) {
194+
count++;
195+
}
196+
}
197+
198+
return count;
199+
}
82200

83201

84202

@@ -106,7 +224,35 @@ const matrix = [
106224
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107225
];
108226

109-
function greatestProduct() {}
227+
function greatestProduct(matrix) {
228+
if (!matrix || matrix.length === 0) {
229+
return null;
230+
}
231+
232+
const rows = matrix.length;
233+
const cols = matrix[0].length;
234+
let maxProduct = 0;
235+
236+
for (let i = 0; i < rows; i++) {
237+
for (let j = 0; j <= cols - 4; j++) {
238+
let product = matrix[i][j] * matrix[i][j+1] * matrix[i][j+2] * matrix[i][j+3];
239+
if (product > maxProduct) {
240+
maxProduct = product;
241+
}
242+
}
243+
}
244+
245+
for (let i = 0; i <= rows - 4; i++) {
246+
for (let j = 0; j < cols; j++) {
247+
let product = matrix[i][j] * matrix[i+1][j] * matrix[i+2][j] * matrix[i+3][j];
248+
if (product > maxProduct) {
249+
maxProduct = product;
250+
}
251+
}
252+
}
253+
254+
return maxProduct;
255+
}
110256

111257

112258

@@ -127,4 +273,4 @@ if (typeof module !== 'undefined') {
127273
howManyTimes,
128274
greatestProduct
129275
};
130-
}
276+
}

tests/functions-and-arrays.spec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ describe('Bonus: Calculate the sum', () => {
120120
});
121121

122122
it('should throw an error when unsupported data type (object or array) present in the array', () => {
123-
expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow(
124-
new Error("Unsupported data type sir or ma'am")
125-
);
123+
expect(() => sum([6, 12, 'miami', 1, 'barca', '200', 'lisboa', 8, [], {}])).toThrow();
126124
});
127125

128126
});

0 commit comments

Comments
 (0)