Skip to content

Commit f0deb5b

Browse files
committed
Solved lab
1 parent bbd5108 commit f0deb5b

File tree

1 file changed

+97
-12
lines changed

1 file changed

+97
-12
lines changed

src/functions-and-arrays.js

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,93 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
2+
function maxOfTwoNumbers(numberOne, numberTwo) {
3+
if(numberOne>numberTwo){
4+
return numberOne;
5+
}
6+
return numberTwo;
7+
}
38

49

510

611
// Iteration #2: Find longest word
712
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
8-
9-
function findLongestWord() {}
10-
13+
let testLng = 0;
14+
let longest;
15+
function findLongestWord(arrLongest) {
16+
for(let i=0; i <= arrLongest.length; i++){
17+
if(arrLongest.length === 0){
18+
return null;
19+
}
20+
if(arrLongest.length === 1){
21+
return arrLongest[i];
22+
}
23+
if(arrLongest.length > 1){
24+
return arrLongest.reduce((longestItem, currentItem) => {
25+
if(longestItem.length === currentItem){
26+
return longestItem;
27+
}
28+
if(longestItem.length < currentItem.length){
29+
return currentItem;
30+
}
31+
return longestItem;
32+
}, "");
33+
}
34+
}
35+
36+
}
37+
1138

1239

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

16-
function sumNumbers() {}
17-
43+
function sumNumbers(arr) {
44+
let sum = 0;
45+
arr.forEach((item) => {
46+
sum = sum + item;
47+
});
48+
return sum;
49+
}
1850

1951

2052
// Iteration #3.1 Bonus:
21-
function sum() {}
53+
function sum(arr) {
54+
let finalSum = 0;
55+
arr.forEach((item) => {
56+
finalSum = finalSum + item.length;
57+
});
58+
return finalSum;
59+
}
60+
const mixedArr = [6, 12, 'miami', 1, true, 'barca', '200', 'lisboa', 8, 10];
2261

2362

2463

2564
// Iteration #4: Calculate the average
2665
// Level 1: Array of numbers
2766
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2867

29-
function averageNumbers() {}
68+
function averageNumbers(arr) {
69+
if(arr.length>0){
70+
let avg = sumNumbers(arr)/arr.length;
71+
return avg;
72+
}
73+
return null;
74+
}
3075

3176

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

35-
function averageWordLength() { }
80+
function averageWordLength(arr) {
81+
let arrSum = '';
82+
arr.forEach((string) => {
83+
arrSum = arrSum + string;
84+
});
85+
let averageOfStrings = arrSum.length/arr.length;
86+
if(averageOfStrings>0){
87+
return averageOfStrings;
88+
}
89+
return null;
90+
}
3691

3792
// Bonus - Iteration #4.1
3893
function avg() {}
@@ -52,14 +107,34 @@ const wordsUnique = [
52107
'bring'
53108
];
54109

55-
function uniquifyArray() {}
110+
function uniquifyArray(arr) {
111+
if(arr.length === 0){
112+
return null;
113+
}
114+
return Array.from(new Set(arr));
115+
}
116+
56117

57118

58119

59120
// Iteration #6: Find elements
60121
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61122

62-
function doesWordExist() {}
123+
function doesWordExist(arrWords) {
124+
for(let i= 0; i <= arrWords; i++){
125+
if(arrWords.length === 0){
126+
return null;
127+
}
128+
if(arrWords >= 1){
129+
return arr.reduce((magicWord, word) => {
130+
if(magicWord === word){
131+
return true;
132+
}
133+
}, "");
134+
}
135+
} return false;
136+
}
137+
63138

64139

65140

@@ -78,7 +153,17 @@ const wordsCount = [
78153
'matter'
79154
];
80155

81-
function howManyTimes() {}
156+
function howManyTimes(count) {
157+
for(let i= 0; i<= count.length; i++){
158+
let sumCount = 0;
159+
if(count.length === 0){
160+
return 0;
161+
}
162+
if(count[i] === i+1){
163+
return
164+
}
165+
}
166+
}
82167

83168

84169

0 commit comments

Comments
 (0)