Skip to content

Commit 4fb4800

Browse files
committed
Ejercicio resuelto menos una parte del ironhack-labs#6 y bonus final
1 parent 4cb03c7 commit 4fb4800

File tree

1 file changed

+243
-67
lines changed

1 file changed

+243
-67
lines changed

src/functions-and-arrays.js

Lines changed: 243 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,295 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
4-
2+
function maxOfTwoNumbers(num1, num2) {
3+
if (num1 > num2) {
4+
return num1;
5+
} else {
6+
return num2;
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+
const words = [
12+
"mystery",
13+
"brother",
14+
"aviator",
15+
"crocodile",
16+
"pearl",
17+
"orchard",
18+
"crackpot",
19+
];
1120

21+
function findLongestWord(arrWords) {
22+
if (arrWords.length === 0) {
23+
return null;
24+
}
25+
if (arrWords.length === 1) {
26+
return arrWords[0];
27+
}
28+
29+
let longestWord = "";
30+
arrWords.forEach(function (word, index) {
31+
if (longestWord.length < word.length) {
32+
longestWord = word;
33+
}
34+
});
35+
return longestWord;
36+
}
1237

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

16-
function sumNumbers() {}
17-
18-
41+
function sumNumbers(arrNumbers) {
42+
if (arrNumbers.length === 0) {
43+
return 0;
44+
}
45+
let sum = 0;
46+
for (let i = 0; i < arrNumbers.length; i++) {
47+
sum += arrNumbers[i];
48+
}
49+
return sum;
50+
}
1951

2052
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
23-
53+
function sum(arr) {
54+
if (arr.length === 0) {
55+
return 0;
56+
}
57+
58+
let sum = 0;
59+
arr.forEach(function (item, i) {
60+
if (typeof item === "number") {
61+
sum += item;
62+
} else if (typeof item === "string") {
63+
sum += item.length;
64+
} else if (typeof item === "boolean" && item === true) {
65+
sum += 1;
66+
} else if (typeof item === "boolean" && item === false) {
67+
sum;
68+
} else {
69+
throw new Error("Unsupported data type sir or ma'am")
70+
}
71+
});
72+
73+
return sum;
74+
}
2475

2576
// Iteration #4: Calculate the average
2677
// Level 1: Array of numbers
2778
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2879

29-
function averageNumbers() {}
30-
80+
function averageNumbers(arrNumbers) {
81+
if (arrNumbers.length === 0) {
82+
return null;
83+
}
84+
let sum = 0;
85+
arrNumbers.forEach(function (number, i) {
86+
sum += number;
87+
});
88+
average = sum / arrNumbers.length;
89+
return average;
90+
}
3191

3292
// Level 2: Array of strings
33-
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
93+
const wordsArr = [
94+
"seat",
95+
"correspond",
96+
"linen",
97+
"motif",
98+
"hole",
99+
"smell",
100+
"smart",
101+
"chaos",
102+
"fuel",
103+
"palace",
104+
];
34105

35-
function averageWordLength() { }
106+
function averageWordLength(arrStrings) {
107+
if (arrStrings.length === 0) {
108+
return null;
109+
}
110+
let sum = 0;
111+
arrStrings.forEach(function (str, i) {
112+
sum += str.length;
113+
});
114+
average = sum / arrStrings.length;
115+
return average;
116+
}
36117

37118
// Bonus - Iteration #4.1
38-
function avg() {}
119+
function avg(arr) {
120+
if (arr.length === 0) {
121+
return null;
122+
}
123+
let sum = 0;
124+
arr.forEach(function (item, i) {
125+
if (typeof item === "number") {
126+
sum += item;
127+
} else if (typeof item === "string") {
128+
sum += item.length;
129+
} else if (typeof item === "boolean" && item === true) {
130+
sum += 1;
131+
} else if (typeof item === "boolean" && item === false) {
132+
sum;
133+
}
134+
});
135+
average = sum / arr.length;
136+
return average;
137+
}
39138

40139
// Iteration #5: Unique arrays
41140
const wordsUnique = [
42-
'crab',
43-
'poison',
44-
'contagious',
45-
'simple',
46-
'bring',
47-
'sharp',
48-
'playground',
49-
'poison',
50-
'communion',
51-
'simple',
52-
'bring'
141+
"crab",
142+
"poison",
143+
"contagious",
144+
"simple",
145+
"bring",
146+
"sharp",
147+
"playground",
148+
"poison",
149+
"communion",
150+
"simple",
151+
"bring",
53152
];
54153

55-
function uniquifyArray() {}
56-
57-
154+
function uniquifyArray(unifArr) {
155+
if (unifArr.length === 0) {
156+
return null;
157+
}
158+
let newArr = [];
159+
unifArr.forEach(function(word, i){
160+
if (!newArr.includes(word)){
161+
newArr.push(word);
162+
}
163+
})
164+
return newArr;
165+
}
58166

59167
// Iteration #6: Find elements
60-
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61-
62-
function doesWordExist() {}
63-
168+
const wordsFind = [
169+
"machine",
170+
"subset",
171+
"trouble",
172+
"starting",
173+
"matter",
174+
"eating",
175+
"truth",
176+
"disobedience",
177+
];
64178

179+
function doesWordExist(wordsFind, word) {
180+
if (wordsFind.length === 0) {
181+
return null;
182+
}
183+
for (let i = 0 ; i < wordsFind.length ; i++){
184+
if (!wordsFind[i].includes(word)){
185+
return false;
186+
} else {
187+
return true;
188+
}
189+
}
190+
}
65191

66192
// Iteration #7: Count repetition
67193
const wordsCount = [
68-
'machine',
69-
'matter',
70-
'subset',
71-
'trouble',
72-
'starting',
73-
'matter',
74-
'eating',
75-
'matter',
76-
'truth',
77-
'disobedience',
78-
'matter'
194+
"machine",
195+
"matter",
196+
"subset",
197+
"trouble",
198+
"starting",
199+
"matter",
200+
"eating",
201+
"matter",
202+
"truth",
203+
"disobedience",
204+
"matter",
79205
];
80206

81-
function howManyTimes() {}
207+
function howManyTimes(wordsCount, word) {
208+
if (wordsCount.length === 0) {
209+
return 0
210+
}
82211

212+
let count = 0
213+
wordsCount.forEach(function (str) {
214+
if (str === word) {
215+
count += 1
216+
}
217+
})
83218

219+
return count
220+
}
84221

85222
// Iteration #8: Bonus
86223
const matrix = [
87224
[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
88-
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
89-
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
225+
[
226+
49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62,
227+
0,
228+
],
229+
[
230+
81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36,
231+
65,
232+
],
90233
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
91-
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
92-
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
93-
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
94-
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
95-
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
234+
[
235+
22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13,
236+
80,
237+
],
238+
[
239+
24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12,
240+
50,
241+
],
242+
[
243+
32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64,
244+
70,
245+
],
246+
[
247+
67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94,
248+
21,
249+
],
250+
[
251+
24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63,
252+
72,
253+
],
96254
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
97255
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
98-
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
256+
[
257+
16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85,
258+
57,
259+
],
99260
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
100-
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
261+
[
262+
19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55,
263+
40,
264+
],
101265
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
102-
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
103-
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
104-
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
105-
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
106-
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
266+
[
267+
88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53,
268+
69,
269+
],
270+
[
271+
4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76,
272+
36,
273+
],
274+
[
275+
20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36,
276+
16,
277+
],
278+
[
279+
20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5,
280+
54,
281+
],
282+
[
283+
1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67,
284+
48,
285+
],
107286
];
108287

109288
function greatestProduct() {}
110289

111-
112-
113-
114290
// The following is required to make unit tests work.
115291
/* Environment setup. Do not modify the below code. */
116-
if (typeof module !== 'undefined') {
292+
if (typeof module !== "undefined") {
117293
module.exports = {
118294
maxOfTwoNumbers,
119295
findLongestWord,
@@ -125,6 +301,6 @@ if (typeof module !== 'undefined') {
125301
uniquifyArray,
126302
doesWordExist,
127303
howManyTimes,
128-
greatestProduct
304+
greatestProduct,
129305
};
130306
}

0 commit comments

Comments
 (0)