1
1
// 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 ) ) ;
4
12
5
13
6
14
// Iteration #2: Find longest word
7
15
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
16
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
+
10
32
11
33
12
34
13
35
// Iteration #3: Calculate the sum
14
36
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
37
+ function sumNumbers ( numbers ) {
38
+ let sum = 0 ;
15
39
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 ) ) ;
18
46
19
47
20
48
// Iteration #3.1 Bonus:
@@ -26,13 +54,38 @@ function sum() {}
26
54
// Level 1: Array of numbers
27
55
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
56
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 ) ) ;
31
71
32
72
// Level 2: Array of strings
33
73
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
74
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 ) ) ;
36
89
37
90
// Bonus - Iteration #4.1
38
91
function avg ( ) { }
@@ -52,19 +105,45 @@ const wordsUnique = [
52
105
'bring'
53
106
] ;
54
107
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 ) ) ;
56
122
57
123
58
124
59
125
// Iteration #6: Find elements
60
126
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 ) ) ;
64
143
65
144
66
145
// Iteration #7: Count repetition
67
- const wordsCount = [
146
+ const myWordsArray = [
68
147
'machine' ,
69
148
'matter' ,
70
149
'subset' ,
@@ -78,9 +157,23 @@ const wordsCount = [
78
157
'matter'
79
158
] ;
80
159
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
+ }
83
175
176
+ console . log ( howManyTimes ( myWordsArray , mySearchWord ) ) ;
84
177
85
178
// Iteration #8: Bonus
86
179
const matrix = [
@@ -110,7 +203,6 @@ function greatestProduct() {}
110
203
111
204
112
205
113
-
114
206
// The following is required to make unit tests work.
115
207
/* Environment setup. Do not modify the below code. */
116
208
if ( typeof module !== 'undefined' ) {
0 commit comments