Skip to content

Commit 7f13c28

Browse files
committed
Finally done
1 parent 8ff766d commit 7f13c28

File tree

1 file changed

+108
-16
lines changed

1 file changed

+108
-16
lines changed

src/functions-and-arrays.js

Lines changed: 108 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,48 @@
11
// Iteration #1: Find the maximum
2-
function maxOfTwoNumbers() {}
3-
2+
function maxOfTwoNumbers(num1, num2) {
3+
if (num1 < num2) {
4+
return num2;
5+
} else if (num1 > num2) {
6+
return num1;
7+
} else {
8+
return num1;
9+
}
10+
}
11+
console.log(maxOfTwoNumbers(23, 45));
412

513

614
// Iteration #2: Find longest word
715
const words = ['mystery', 'brother', 'aviator', 'crocodile', 'pearl', 'orchard', 'crackpot'];
816

9-
function findLongestWord() {}
17+
function findLongestWord(words) {
18+
if (words.length === 0) {
19+
return null;
20+
}
21+
22+
let longestWord = words[0];
23+
for (let i = 1; i < words.length; i++) {
24+
if (words[i].length > longestWord.length) {
25+
longestWord = words[i];
26+
}
27+
}
28+
return longestWord;
29+
}
30+
console.log(findLongestWord(words));
31+
1032

1133

1234

1335
// Iteration #3: Calculate the sum
1436
const numbers = [6, 12, 1, 18, 13, 16, 2, 1, 8, 10];
37+
function sumNumbers(numbers) {
38+
let sum = 0;
1539

16-
function sumNumbers() {}
17-
40+
for (let i = 0; i < numbers.length; i++) {
41+
sum += numbers[i];
42+
}
43+
return sum;
44+
}
45+
console.log(sumNumbers(numbers));
1846

1947

2048
// Iteration #3.1 Bonus:
@@ -26,13 +54,38 @@ function sum() {}
2654
// Level 1: Array of numbers
2755
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
2856

29-
function averageNumbers() {}
30-
57+
function averageNumbers(numbersAvg) {
58+
if (numbersAvg.length === 0) {
59+
return null;
60+
}
61+
62+
let sum = 0;
63+
for (let i = 0; i < numbersAvg.length; i++) {
64+
sum += numbersAvg[i];
65+
}
66+
const average = sum / numbersAvg.length;
67+
console.log(average);
68+
return average;
69+
}
70+
console.log(averageNumbers(numbersAvg));
3171

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

35-
function averageWordLength() { }
75+
function averageWordLength(wordsArr) {
76+
if (wordsArr.length === 0) {
77+
return null;
78+
}
79+
80+
let total = 0;
81+
for (let i = 0; i < wordsArr.length; i++) {
82+
total += wordsArr[i].length;
83+
}
84+
const average = total / wordsArr.length;
85+
console.log(average);
86+
return average;
87+
}
88+
console.log(averageWordLength(wordsArr));
3689

3790
// Bonus - Iteration #4.1
3891
function avg() {}
@@ -52,19 +105,45 @@ const wordsUnique = [
52105
'bring'
53106
];
54107

55-
function uniquifyArray() {}
108+
function uniquifyArray(wordsUnique) {
109+
if (wordsUnique.length === 0) {
110+
return null;
111+
}
112+
113+
let unique = [];
114+
for (i = 0; i < wordsUnique.length; i++) {
115+
if(unique.indexOf(wordsUnique[i]) === -1) {
116+
unique.push(wordsUnique[i]);
117+
}
118+
}
119+
return unique;
120+
}
121+
console.log(uniquifyArray(wordsUnique));
56122

57123

58124

59125
// Iteration #6: Find elements
60126
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
61-
62-
function doesWordExist() {}
63-
127+
let searchWord = "machine";
128+
function doesWordExist(wordsFind, searchWord) {
129+
if (wordsFind.length === 0) {
130+
return null;
131+
}
132+
for (i = 0; i < wordsFind.length; i++) {
133+
if(wordsFind[i] === searchWord) {
134+
return true;
135+
}
136+
}
137+
// } else if (wordsFind.indexOf(searchWord) !== -1) {
138+
// return true;
139+
// }
140+
return false;
141+
}
142+
console.log(doesWordExist(wordsFind, searchWord));
64143

65144

66145
// Iteration #7: Count repetition
67-
const wordsCount = [
146+
const myWordsArray = [
68147
'machine',
69148
'matter',
70149
'subset',
@@ -78,9 +157,23 @@ const wordsCount = [
78157
'matter'
79158
];
80159

81-
function howManyTimes() {}
82-
160+
const mySearchWord = "matter";
161+
162+
function howManyTimes(myWordsArray, mySearchWord) {
163+
if (myWordsArray.length === 0) {
164+
return 0;
165+
}
166+
// falls Array iwelche Werte hat, dann macht er weiter
167+
let wordCount = 0; // wir declarieren eine variable um zu verstehen wir oft wird das gesuchte Wort vorkommen
168+
for (i = 0; i < myWordsArray.length; i++){ // hier loopen wir jeden einzelnen Item durch bis es keinen mehr gibt
169+
if(myWordsArray[i] === mySearchWord) { // hier wird es geprueft ob [i] = dem gesuchten Wort entspricht (zB in dem Fall 'machine' = 'matter')
170+
wordCount += 1; // wenn das Wort gefunden wird (bei jedem Treffer wird +1 gerechnet)
171+
}
172+
}
173+
return wordCount;
174+
}
83175

176+
console.log(howManyTimes(myWordsArray, mySearchWord));
84177

85178
// Iteration #8: Bonus
86179
const matrix = [
@@ -110,7 +203,6 @@ function greatestProduct() {}
110203

111204

112205

113-
114206
// The following is required to make unit tests work.
115207
/* Environment setup. Do not modify the below code. */
116208
if (typeof module !== 'undefined') {

0 commit comments

Comments
 (0)