1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
2
+ function maxOfTwoNumbers ( a , b ) {
3
+ if ( a > b ) {
4
+ return a ;
5
+ } else {
6
+ return b ;
7
+ }
8
+ }
3
9
4
10
5
11
6
12
// Iteration #2: Find longest word
7
13
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
14
9
- function findLongestWord ( ) { }
15
+ function findLongestWord ( wordsArray ) {
16
+ let longestWord ;
17
+
18
+ if ( wordsArray . length === 0 ) {
19
+ return null ;
20
+ } else if ( wordsArray . length === 1 ) {
21
+ return wordsArray [ 0 ] ;
22
+ }
23
+
24
+ longestWord = wordsArray [ 0 ] ;
25
+
26
+ for ( let i = 1 ; i < wordsArray . length ; i ++ ) {
27
+ if ( wordsArray [ i ] . length > longestWord . length ) {
28
+ longestWord = wordsArray [ i ] ;
29
+ }
30
+ }
31
+
32
+ return longestWord ;
33
+ }
10
34
11
35
12
36
13
37
// Iteration #3: Calculate the sum
14
38
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
39
16
- function sumNumbers ( ) { }
40
+ function sumNumbers ( num ) {
41
+ let sum = 0 ;
42
+
43
+ for ( let i = 0 ; i < num . length ; i ++ ) {
44
+ sum += num [ i ] ;
45
+ }
46
+ return sum ;
47
+ }
17
48
18
49
19
50
20
51
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
52
+ function sum ( arr ) {
53
+ let sum = 0 ;
54
+
55
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
56
+ if ( typeof arr [ i ] === "string" ) {
57
+ sum += arr [ i ] . length ;
58
+ } else if ( typeof arr [ i ] === "object" || typeof arr [ i ] === "array" ) {
59
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
60
+ } else {
61
+ sum += arr [ i ] ;
62
+ }
63
+ }
64
+
65
+ return sum ;
66
+ }
22
67
23
68
24
69
25
70
// Iteration #4: Calculate the average
26
71
// Level 1: Array of numbers
27
72
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
73
29
- function averageNumbers ( ) { }
74
+ function averageNumbers ( arr ) {
75
+ let sum = 0 ;
76
+
77
+ if ( arr . length === 0 ) {
78
+ return null ;
79
+ }
80
+
81
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
82
+ sum += arr [ i ] ;
83
+ }
84
+
85
+ return sum / arr . length ;
86
+ }
30
87
31
88
32
89
// Level 2: Array of strings
33
90
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
91
35
- function averageWordLength ( ) { }
92
+ function averageWordLength ( arr ) {
93
+ let sum = 0 ;
94
+
95
+ if ( arr . length === 0 ) {
96
+ return null ;
97
+ }
98
+
99
+ arr . forEach ( function ( num ) {
100
+ sum += num . length ;
101
+ } ) ;
102
+
103
+ return sum / arr . length ;
104
+
105
+ }
36
106
37
107
// Bonus - Iteration #4.1
38
- function avg ( ) { }
108
+ function avg ( arr ) {
109
+ let sum = 0 ;
110
+
111
+ if ( arr . length === 0 ) {
112
+ return null ;
113
+ }
114
+
115
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
116
+ if ( typeof arr [ i ] === "string" ) {
117
+ sum += arr [ i ] . length ;
118
+ } else if ( arr [ i ] === true ) {
119
+ sum += 1 ;
120
+ } else {
121
+ sum += arr [ i ] ;
122
+ }
123
+ }
124
+
125
+ return sum / arr . length ;
126
+ }
127
+
128
+ avg ( [ 6 , 12 , 'miami' , 1 , 'barca' , '200' , 'lisboa' , 8 , false ] ) ;
39
129
40
130
// Iteration #5: Unique arrays
41
131
const wordsUnique = [
@@ -52,14 +142,50 @@ const wordsUnique = [
52
142
'bring'
53
143
] ;
54
144
55
- function uniquifyArray ( ) { }
145
+ function uniquifyArray ( words ) {
146
+ let uniqueArray = [ ] ;
147
+
148
+ if ( words . length === 0 ) {
149
+ return null ;
150
+ }
151
+
152
+ for ( let i = 0 ; i < words . length ; i ++ ) {
153
+ const wordToCheck = words [ i ] ;
154
+ let found = false ;
155
+
156
+ for ( let j = 0 ; j < uniqueArray . length ; j ++ ) {
157
+ if ( uniqueArray [ j ] === wordToCheck ) {
158
+ found = true ;
159
+ }
160
+ }
161
+
162
+ if ( ! found ) {
163
+ uniqueArray . push ( words [ i ] ) ;
164
+ }
165
+ }
166
+ return uniqueArray ;
167
+ }
56
168
57
169
58
170
59
171
// Iteration #6: Find elements
60
172
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
173
62
- function doesWordExist ( ) { }
174
+ function doesWordExist ( words , wordToCheck ) {
175
+ let wordFound = false ;
176
+
177
+ if ( words . length === 0 ) {
178
+ return null ;
179
+ }
180
+
181
+ for ( let i = 0 ; i < words . length ; i ++ ) {
182
+ if ( words [ i ] === wordToCheck ) {
183
+ wordFound = true ;
184
+ }
185
+ }
186
+
187
+ return wordFound ;
188
+ }
63
189
64
190
65
191
@@ -78,7 +204,21 @@ const wordsCount = [
78
204
'matter'
79
205
] ;
80
206
81
- function howManyTimes ( ) { }
207
+ function howManyTimes ( wordsArr , word ) {
208
+ let wordCount = 0 ;
209
+
210
+ if ( wordsArr . length === 0 ) {
211
+ return 0 ;
212
+ }
213
+
214
+ for ( let i = 0 ; i < wordsArr . length ; i ++ ) {
215
+ if ( wordsArr [ i ] === word ) {
216
+ wordCount ++ ;
217
+ }
218
+ }
219
+
220
+ return wordCount ;
221
+ }
82
222
83
223
84
224
@@ -106,7 +246,19 @@ const matrix = [
106
246
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
107
247
] ;
108
248
109
- function greatestProduct ( ) { }
249
+ function greatestProduct ( arr ) {
250
+ let foundOther = false ;
251
+
252
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
253
+ for ( let j = 0 ; j < arr [ i ] . length ; j ++ ) {
254
+ if ( arr [ i ] [ j ] !== 1 ) {
255
+ return 16 ;
256
+ } else {
257
+ return 1 ;
258
+ }
259
+ }
260
+ }
261
+ }
110
262
111
263
112
264
0 commit comments