Skip to content

Commit 61a3e92

Browse files
committed
feat: solve iterations 1-7
1 parent 2d06cee commit 61a3e92

File tree

1 file changed

+108
-17
lines changed

1 file changed

+108
-17
lines changed

src/functions-and-arrays.js

Lines changed: 108 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,106 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
2+
function maxOfTwoNumbers(a, b) {
3+
return Math.max(a, b);
4+
}
45

56

67
// Iteration #2: Find longest word
78
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
89

9-
function findLongestWord() {}
10-
10+
function findLongestWord(words) {
11+
if (!words.length) {
12+
return null;
13+
} else if (words.length === 1) {
14+
return words[0];
15+
} else {
16+
return words.sort((a, b ) => b.length - a.length)[0];
17+
}
18+
}
1119

1220

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

16-
function sumNumbers() {}
17-
24+
function sumNumbers(numbers) {
25+
let sum = 0;
26+
for (let number of numbers) {
27+
sum += number;
28+
}
29+
return sum;
30+
}
1831

1932

2033
// Iteration #3.1 Bonus:
21-
function sum() {}
22-
34+
function sum(dataArray) {
35+
let sum = 0;
36+
for (let element of dataArray) {
37+
switch (typeof element) {
38+
case "number":
39+
case "boolean":
40+
sum += element;
41+
break;
42+
case "string":
43+
sum += element.length;
44+
break;
45+
default:
46+
throw new Error("Unsupported data type sir or ma'am");
47+
break;
48+
}
49+
}
50+
return sum;
51+
}
2352

2453

2554
// Iteration #4: Calculate the average
2655
// Level 1: Array of numbers
2756
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2857

29-
function averageNumbers() {}
30-
58+
function averageNumbers(numbers) {
59+
if (numbers.length) {
60+
return sumNumbers(numbers) / numbers.length;
61+
} else {
62+
return null;
63+
}
64+
}
3165

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

35-
function averageWordLength() { }
69+
function averageWordLength(stringArray) {
70+
if (stringArray.length) {
71+
sum = 0;
72+
for (let string of stringArray) {
73+
sum += string.length;
74+
}
75+
return parseInt(sum / stringArray.length);
76+
} else {
77+
return null;
78+
}
79+
}
3680

3781
// Bonus - Iteration #4.1
38-
function avg() {}
82+
function avg(dataArray) {
83+
if (dataArray.length) {
84+
let sum = 0;
85+
for (let element of dataArray) {
86+
switch (typeof element) {
87+
case "number":
88+
case "boolean":
89+
sum += element;
90+
break;
91+
case "string":
92+
sum += element.length;
93+
break;
94+
default:
95+
throw new Error("Unsupported data type sir or ma'am");
96+
break;
97+
}
98+
}
99+
return sum / dataArray.length;
100+
} else {
101+
return null;
102+
}
103+
}
39104

40105
// Iteration #5: Unique arrays
41106
const wordsUnique = [
@@ -52,15 +117,27 @@ const wordsUnique = [
52117
'bring'
53118
];
54119

55-
function uniquifyArray() {}
120+
function uniquifyArray(array) {
121+
if(array.length) {
122+
let cleanedArray = [];
123+
for (let element of array) {
124+
if (!cleanedArray.includes(element)) {
125+
cleanedArray.push(element);
126+
}
127+
} return cleanedArray;
128+
} else {
129+
return null;
130+
}
131+
}
56132

57133

58134

59135
// Iteration #6: Find elements
60136
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61137

62-
function doesWordExist() {}
63-
138+
function doesWordExist(wordList, searchWord) {
139+
return (wordList.length) ? wordList.includes(searchWord) : null;
140+
}
64141

65142

66143
// Iteration #7: Count repetition
@@ -78,8 +155,22 @@ const wordsCount = [
78155
'matter'
79156
];
80157

81-
function howManyTimes() {}
82-
158+
function howManyTimes(wordList, searchWord) {
159+
if (wordList.length) {
160+
let occurances = 0;
161+
for (let word of wordList) {
162+
if (word === searchWord) {
163+
console.log(word);
164+
occurances++;
165+
} else {
166+
continue;
167+
}
168+
}
169+
return occurances;
170+
} else {
171+
return 0;
172+
}
173+
}
83174

84175

85176
// Iteration #8: Bonus

0 commit comments

Comments
 (0)