Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1b8338c

Browse files
committedFeb 15, 2025
Solved lab
1 parent 8ff766d commit 1b8338c

File tree

1 file changed

+171
-13
lines changed

1 file changed

+171
-13
lines changed
 

‎src/functions-and-arrays.js

+171-13
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,158 @@
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(words) {
16+
if (words.length === 0) {
17+
return null;
18+
}
19+
let longestWord = '';
20+
for (let i = 0; i < words.length; i++) {
21+
if (words[i].length > longestWord.length) {
22+
longestWord = words[i];
23+
}
24+
}
25+
return longestWord;
26+
}
1027

1128

1229

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

16-
function sumNumbers() {}
33+
function sumNumbers(numbers) {
34+
if (numbers.length === 0) {
35+
return 0;
36+
}
37+
let sum = 0;
38+
for (let i = 0; i < numbers.length; i++) {
39+
sum += numbers[i]
40+
}
41+
return sum;
42+
}
43+
44+
console.log(sumNumbers(numbers));
1745

1846

1947

20-
// Iteration #3.1 Bonus:
21-
function sum() {}
48+
// Iteration #3.2 Bonus:
49+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
50+
51+
function checkAllZero(mixedArr) {
52+
for (let i = 0; i < mixedArr.length; i++) {
53+
if (mixedArr[i] !== 0) {
54+
return false;
55+
}
56+
}
57+
return true;
58+
}
59+
60+
function sum(mixedArr) {
61+
if (mixedArr.length === 0) {
62+
return 0;
63+
}
64+
if (checkAllZero(mixedArr)) {
65+
return 0;
66+
}
67+
let sum = 0;
68+
for (let i = 0; i < mixedArr.length; i++) {
69+
if (typeof mixedArr[i] === "number") {
70+
sum += mixedArr[i];
71+
}
72+
else if (typeof mixedArr[i] === "string") {
73+
sum += mixedArr[i].length;
74+
}
75+
else if (typeof mixedArr[i] === "boolean") {
76+
if (mixedArr[i] === true) {
77+
sum += 1;
78+
} else {
79+
sum += 0;
80+
}
81+
} else {
82+
throw new Error("Unsupported data type");
83+
}
84+
}
85+
return sum;
86+
}
2287

2388

2489

2590
// Iteration #4: Calculate the average
2691
// Level 1: Array of numbers
2792
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2893

29-
function averageNumbers() {}
94+
function averageNumbers(numbersAvg) {
95+
if (numbersAvg.length === 0) {
96+
return null;
97+
}
98+
let sum = 0;
99+
for (let i = 0; i < numbersAvg.length; i++) {
100+
sum += numbersAvg[i]
101+
}
102+
return sum / numbersAvg.length;
103+
}
104+
105+
console.log(averageNumbers(numbersAvg));
30106

31107

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

35-
function averageWordLength() { }
111+
function averageWordLength(wordsArr) {
112+
if (wordsArr.length === 0) {
113+
return null;
114+
}
115+
116+
let wordLength = 0;
117+
let avgwordLength = 0;
118+
for (let i = 0; i < wordsArr.length; i++) {
119+
wordLength = wordsArr[i].length;
120+
avgwordLength += wordLength;
121+
}
122+
return avgwordLength / wordsArr.length;
123+
}
124+
125+
126+
// Bonus - Iteration #4.3
127+
const arr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
128+
function avg(arr) {
129+
if (arr.length === 0) {
130+
return null;
131+
}
132+
let sum = 0;
133+
for (let i = 0; i < arr.length; i++) {
134+
if (typeof arr[i] === "number") {
135+
sum += arr[i];
136+
}
137+
else if (typeof arr[i] === "string") {
138+
sum += arr[i].length;
139+
}
140+
else if (typeof arr[i] === "boolean") {
141+
if (arr[i] === true) {
142+
sum += 1;
143+
} else {
144+
sum += 0;
145+
}
146+
} else {
147+
throw new Error("Unsupported data type");
148+
}
149+
}
150+
return sum / arr.length;
151+
}
152+
153+
console.log(avg(arr));
154+
36155

37-
// Bonus - Iteration #4.1
38-
function avg() {}
39156

40157
// Iteration #5: Unique arrays
41158
const wordsUnique = [
@@ -52,14 +169,41 @@ const wordsUnique = [
52169
'bring'
53170
];
54171

55-
function uniquifyArray() {}
172+
function uniquifyArray(wordsUnique) {
173+
if (wordsUnique.length === 0) {
174+
return null;
175+
}
176+
let uniqueArray = [];
177+
for (let i = 0; i < wordsUnique.length; i++) {
178+
if (!uniqueArray.includes(wordsUnique[i])) {
179+
uniqueArray.push(wordsUnique[i]);
180+
}
181+
}
182+
return uniqueArray;
183+
}
56184

57185

58186

59187
// Iteration #6: Find elements
60188
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61189

62-
function doesWordExist() {}
190+
function doesWordExist(wordsFind, wordToFind) {
191+
if (wordsFind.length === 0) {
192+
return null;
193+
}
194+
195+
let isFounded = false;
196+
197+
for (let i = 0; i < wordsFind.length; i++) {
198+
if (wordsFind[i] === wordToFind) {
199+
isFounded = true;
200+
}
201+
}
202+
203+
return isFounded;
204+
}
205+
206+
console.log(doesWordExist(wordsFind, 'machine'));
63207

64208

65209

@@ -78,8 +222,20 @@ const wordsCount = [
78222
'matter'
79223
];
80224

81-
function howManyTimes() {}
225+
function howManyTimes(wordsCount, wordToSearch) {
226+
if (wordsCount.length === 0) {
227+
return 0;
228+
}
229+
let count = 0;
230+
for (let i = 0; i < wordsCount.length; i++) {
231+
if (wordToSearch === wordsCount[i]) {
232+
count++;
233+
}
234+
}
235+
return count;
236+
}
82237

238+
console.log(doesWordExist(wordsFind, 'matter'));
83239

84240

85241
// Iteration #8: Bonus
@@ -106,7 +262,7 @@ const matrix = [
106262
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
107263
];
108264

109-
function greatestProduct() {}
265+
function greatestProduct() { }
110266

111267

112268

@@ -128,3 +284,5 @@ if (typeof module !== 'undefined') {
128284
greatestProduct
129285
};
130286
}
287+
288+

0 commit comments

Comments
 (0)