Skip to content

Commit 8ef2458

Browse files
committed
Lab Javascript Functions and Arrays until bonus ironhack-labs#7
1 parent 13ac3f3 commit 8ef2458

File tree

1 file changed

+178
-12
lines changed

1 file changed

+178
-12
lines changed

src/functions-and-arrays.js

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

416

517

618
// Iteration #2: Find longest word
719
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
820

9-
function findLongestWord() {}
21+
function findLongestWord(words) {
22+
23+
if (words.length === 0) {
24+
return null;
25+
}
26+
27+
let longestWord = "";
28+
words.forEach(function (word){
29+
if (word.length > longestWord.length) {
30+
longestWord = word;
31+
}
32+
});
33+
34+
return longestWord;
35+
36+
37+
38+
}
1039

1140

1241

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

16-
function sumNumbers() {}
45+
function sumNumbers(arr) {
46+
let sum = 0;
47+
48+
if (arr.length === 0) {
49+
return 0
50+
}
51+
else if (arr.length === 1) {
52+
return arr[0];
53+
54+
//return arr[i].sample //random
55+
}
56+
57+
for (let i = 0; i < arr.length; i++) {
58+
sum += arr[i];
59+
}
60+
return sum;
61+
}
1762

1863

1964

2065
// Iteration #3.1 Bonus:
21-
function sum() {}
66+
function sum(mixedArr) {
67+
if (mixedArr.length === 0) {
68+
return 0;
69+
}
70+
71+
let arrSum = 0;
72+
73+
for (i = 0; i < mixedArr.length ; i++) {
74+
75+
let detectType = typeof mixedArr[i];
76+
77+
switch(detectType) {
78+
case 'number':
79+
arrSum += mixedArr[i];
80+
break;
81+
case 'string':
82+
arrSum += mixedArr[i].length;
83+
break;
84+
case 'boolean':
85+
if ( mixedArr[i] === true ) {
86+
arrSum++ ;
87+
}
88+
break;
89+
default:
90+
throw new Error("Unsupported data type sir or ma'am");
91+
}
92+
}
93+
return arrSum;
94+
}
95+
2296

2397

2498

2599
// Iteration #4: Calculate the average
26100
// Level 1: Array of numbers
27101
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
28102

29-
function averageNumbers() {}
103+
function averageNumbers(arr) {
104+
let sum = 0;
105+
106+
if (arr.length === 0) {
107+
return null;
108+
}
109+
else if (arr.length === 1) {
110+
return arr[0];
111+
//return arr[i].sample //random
112+
}
113+
114+
for (let i = 0; i < arr.length; i++) {
115+
sum += arr[i];
116+
}
117+
return sum/arr.length;
118+
119+
}
120+
30121

31122

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

35-
function averageWordLength() { }
126+
function averageWordLength(wordsArr) {
127+
128+
let averageSum = 0;
129+
130+
if (wordsArr.length === 0) {
131+
return null;
132+
}
133+
134+
else if (wordsArr.length === 1) {
135+
let averageSum = wordsArr[0].length / wordsArr.length;
136+
return averageSum;
137+
}
138+
else {
139+
for (let i = 0; i < wordsArr.length; i++) {
140+
averageSum += wordsArr[i].length; }
141+
142+
let arrAverageSum = averageSum / wordsArr.length;
143+
return arrAverageSum
144+
}
145+
146+
}
147+
148+
36149

37150
// Bonus - Iteration #4.1
38-
function avg() {}
151+
function avg(arr) {
152+
153+
if (arr.length === 0) {
154+
return null;
155+
}
156+
157+
let avgSum = sum(arr) / arr.length
158+
return Number.parseFloat((avgSum).toFixed(2));
159+
160+
}
39161

40162
// Iteration #5: Unique arrays
41163
const wordsUnique = [
@@ -52,15 +174,40 @@ const wordsUnique = [
52174
'bring'
53175
];
54176

55-
function uniquifyArray() {}
177+
function uniquifyArray(words) {
178+
179+
if (words.length === 0){
180+
return null
181+
}
182+
183+
let unique = [];
184+
words.forEach(function (word) {
185+
if (!unique.includes(word)) {
186+
unique.push(word);
187+
}
188+
});
189+
return unique;
190+
}
191+
56192

57193

58194

59195
// Iteration #6: Find elements
60196
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61197

62-
function doesWordExist() {}
63-
198+
function doesWordExist(wordsFind, wordSearch) {
199+
if(wordsFind.length === 0){
200+
return null;
201+
}
202+
203+
for(let i=0 ; i < wordsFind.length; i++){
204+
found = [];
205+
if(wordSearch.includes(wordsFind[i])){
206+
return true;
207+
}
208+
}
209+
return found.includes(wordsFind[wordSearch]);
210+
}
64211

65212

66213
// Iteration #7: Count repetition
@@ -78,7 +225,25 @@ const wordsCount = [
78225
'matter'
79226
];
80227

81-
function howManyTimes() {}
228+
function howManyTimes(words, wordSearch) {
229+
if(words.length === 0){
230+
return 0;
231+
}
232+
233+
//other solution with reduce
234+
// return words.reduce((acc, word) => {
235+
// return (wordSearch === word ? acc + 1 : acc)
236+
// }, 0);
237+
//};
238+
239+
let wordTimes = 0;
240+
words.flat(Infinity).forEach(word => {
241+
if (word === wordSearch) {
242+
wordTimes++
243+
}
244+
})
245+
return wordTimes
246+
}
82247

83248

84249

@@ -106,8 +271,9 @@ const matrix = [
106271
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107272
];
108273

109-
function greatestProduct() {}
274+
function greatestProduct(arr) {
110275

276+
}
111277

112278

113279

0 commit comments

Comments
 (0)