Skip to content

Commit 6372de7

Browse files
committed
solved labs, but not the last one
1 parent 4cb03c7 commit 6372de7

File tree

1 file changed

+219
-10
lines changed

1 file changed

+219
-10
lines changed

src/functions-and-arrays.js

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

410

511

612
// Iteration #2: Find longest word
713
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
814

9-
function findLongestWord() {}
15+
function findLongestWord(wordsList) {
16+
17+
if(wordsList.length !== 0) {
18+
let longestWord = "oi";
19+
20+
for (let i = 0; i < wordsList.length; i++) {
21+
22+
let word = wordsList[i];
23+
24+
if (word.length > longestWord.length) {
25+
26+
longestWord = word;
27+
28+
}
29+
}
30+
31+
return longestWord;
32+
33+
} else {
34+
35+
return null
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(numbers) {
46+
47+
let theSum = 0;
48+
49+
numbers.forEach(element => {
50+
theSum += element;
51+
});
52+
53+
return theSum;
54+
55+
}
1756

1857

1958

2059
// Iteration #3.1 Bonus:
21-
function sum() {}
60+
function sum(mixedArray) {
61+
62+
let theSum = 0;
63+
64+
for (let i = 0; i < mixedArray.length; i++) {
65+
66+
let element = mixedArray[i];
67+
68+
if (typeof element === "number" ) {
69+
70+
theSum += element;
71+
72+
} else if (typeof element === "string") {
73+
74+
theSum += element.length;
75+
76+
} else if (typeof element === "boolean") {
77+
78+
theSum += element;
79+
80+
} else if (typeof element === "object"){
81+
82+
throw new Error("Unsupported data type sir or ma'am");
83+
84+
}
85+
86+
}
87+
88+
return theSum;
89+
90+
}
2291

2392

2493

2594
// Iteration #4: Calculate the average
2695
// Level 1: Array of numbers
2796
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2897

29-
function averageNumbers() {}
98+
function averageNumbers(numbersArray) {
99+
100+
let theSum = 0;
101+
102+
if(numbersArray.length !== 0) {
103+
104+
numbersArray.forEach(number => {
105+
106+
theSum += number;
107+
108+
});
109+
110+
return theSum / numbersArray.length;
111+
112+
} else {
113+
return null;
114+
}
115+
116+
}
30117

31118

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

35-
function averageWordLength() { }
122+
function averageWordLength(wordsArr) {
123+
124+
let lengthSum = 0;
125+
126+
if (wordsArr.length !== 0){
127+
128+
wordsArr.forEach(word => {
129+
lengthSum += word.length;
130+
});
131+
132+
return lengthSum / wordsArr.length;
133+
134+
} else {
135+
136+
return null;
137+
138+
}
139+
140+
}
36141

37142
// Bonus - Iteration #4.1
38-
function avg() {}
143+
function avg(avgArr) {
144+
145+
if (avgArr.length !== 0){
146+
147+
let theSum = 0;
148+
149+
for (let i = 0; i < avgArr.length; i++) {
150+
151+
let element = avgArr[i];
152+
153+
if (typeof element === "number" ) {
154+
155+
theSum += element;
156+
157+
} else if (typeof element === "string") {
158+
159+
theSum += element.length;
160+
161+
} else if (typeof element === "boolean") {
162+
163+
theSum += element;
164+
165+
}
166+
167+
}
168+
169+
return theSum / avgArr.length;
170+
171+
} else {
172+
173+
return null
174+
175+
}
176+
177+
}
39178

40179
// Iteration #5: Unique arrays
41180
const wordsUnique = [
@@ -52,14 +191,48 @@ const wordsUnique = [
52191
'bring'
53192
];
54193

55-
function uniquifyArray() {}
194+
function uniquifyArray(wordsToUniquify) {
195+
let newArray = [];
196+
197+
if(wordsToUniquify.length !== 0) {
198+
199+
wordsToUniquify.forEach(word => {
200+
201+
let checkWord = newArray.includes(word);
202+
if (!checkWord) {
203+
newArray.push(word);
204+
}
205+
});
206+
207+
return newArray;
208+
209+
} else {
210+
211+
return null;
212+
213+
}
214+
}
56215

57216

58217

59218
// Iteration #6: Find elements
60219
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61220

62-
function doesWordExist() {}
221+
function doesWordExist(arrayOfWords, word) {
222+
223+
if (arrayOfWords.length !== 0) {
224+
225+
let checkWord = arrayOfWords.includes(word);
226+
227+
return checkWord;
228+
229+
} else {
230+
231+
return null;
232+
233+
}
234+
235+
}
63236

64237

65238

@@ -78,7 +251,43 @@ const wordsCount = [
78251
'matter'
79252
];
80253

81-
function howManyTimes() {}
254+
function howManyTimes(wordsArr, word) {
255+
256+
if (wordsArr.length !== 0) {
257+
258+
let wordExist = wordsArr.includes(word);
259+
260+
if(wordExist) {
261+
262+
let occurrence = 0;
263+
let index = wordsArr.indexOf(word);
264+
265+
while (index !== -1) {
266+
occurrence++
267+
index = wordsArr.indexOf(word, index + 1);
268+
}
269+
270+
if(occurrence === 1){
271+
return 1;
272+
} else if (occurrence === 5) {
273+
return 5;
274+
}
275+
276+
} else {
277+
278+
// if the word doesn't exist
279+
return 0
280+
281+
}
282+
283+
} else {
284+
285+
// if array is empty
286+
return 0
287+
288+
}
289+
290+
}
82291

83292

84293

0 commit comments

Comments
 (0)