1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
2
+ function maxOfTwoNumbers ( num1 , num2 ) {
3
+
4
+ if ( num1 > num2 ) {
5
+ return num1 ;
6
+ }
7
+ else if ( num2 > num1 ) {
8
+ return num2 ;
9
+ }
10
+ else if ( num1 === num2 ) {
11
+ return ( num1 || num2 ) ;
12
+ }
13
+
14
+ }
3
15
4
16
5
17
6
18
// Iteration #2: Find longest word
7
19
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
20
9
- function findLongestWord ( ) { }
21
+ function findLongestWord ( words ) {
22
+
23
+ if ( words . length === 0 ) {
24
+ return null ;
25
+ }
26
+
27
+ let longestWord = "" ;
28
+ words . forEach ( function ( word ) {
29
+ if ( word . length > longestWord . length ) {
30
+ longestWord = word ;
31
+ }
32
+ } ) ;
33
+
34
+ return longestWord ;
35
+
36
+
37
+
38
+ }
10
39
11
40
12
41
13
42
// Iteration #3: Calculate the sum
14
43
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
44
16
- function sumNumbers ( ) { }
45
+ function sumNumbers ( arr ) {
46
+ let sum = 0 ;
47
+
48
+ if ( arr . length === 0 ) {
49
+ return 0
50
+ }
51
+ else if ( arr . length === 1 ) {
52
+ return arr [ 0 ] ;
53
+
54
+ //return arr[i].sample //random
55
+ }
56
+
57
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
58
+ sum += arr [ i ] ;
59
+ }
60
+ return sum ;
61
+ }
17
62
18
63
19
64
20
65
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
66
+ function sum ( mixedArr ) {
67
+ if ( mixedArr . length === 0 ) {
68
+ return 0 ;
69
+ }
70
+
71
+ let arrSum = 0 ;
72
+
73
+ for ( i = 0 ; i < mixedArr . length ; i ++ ) {
74
+
75
+ let detectType = typeof mixedArr [ i ] ;
76
+
77
+ switch ( detectType ) {
78
+ case 'number' :
79
+ arrSum += mixedArr [ i ] ;
80
+ break ;
81
+ case 'string' :
82
+ arrSum += mixedArr [ i ] . length ;
83
+ break ;
84
+ case 'boolean' :
85
+ if ( mixedArr [ i ] === true ) {
86
+ arrSum ++ ;
87
+ }
88
+ break ;
89
+ default :
90
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
91
+ }
92
+ }
93
+ return arrSum ;
94
+ }
95
+
22
96
23
97
24
98
25
99
// Iteration #4: Calculate the average
26
100
// Level 1: Array of numbers
27
101
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
102
29
- function averageNumbers ( ) { }
103
+ function averageNumbers ( arr ) {
104
+ let sum = 0 ;
105
+
106
+ if ( arr . length === 0 ) {
107
+ return null ;
108
+ }
109
+ else if ( arr . length === 1 ) {
110
+ return arr [ 0 ] ;
111
+ //return arr[i].sample //random
112
+ }
113
+
114
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
115
+ sum += arr [ i ] ;
116
+ }
117
+ return sum / arr . length ;
118
+
119
+ }
120
+
30
121
31
122
32
123
// Level 2: Array of strings
33
124
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
125
35
- function averageWordLength ( ) { }
126
+ function averageWordLength ( wordsArr ) {
127
+
128
+ let averageSum = 0 ;
129
+
130
+ if ( wordsArr . length === 0 ) {
131
+ return null ;
132
+ }
133
+
134
+ else if ( wordsArr . length === 1 ) {
135
+ let averageSum = wordsArr [ 0 ] . length / wordsArr . length ;
136
+ return averageSum ;
137
+ }
138
+ else {
139
+ for ( let i = 0 ; i < wordsArr . length ; i ++ ) {
140
+ averageSum += wordsArr [ i ] . length ; }
141
+
142
+ let arrAverageSum = averageSum / wordsArr . length ;
143
+ return arrAverageSum
144
+ }
145
+
146
+ }
147
+
148
+
36
149
37
150
// Bonus - Iteration #4.1
38
- function avg ( ) { }
151
+ function avg ( arr ) {
152
+
153
+ if ( arr . length === 0 ) {
154
+ return null ;
155
+ }
156
+
157
+ let avgSum = sum ( arr ) / arr . length
158
+ return Number . parseFloat ( ( avgSum ) . toFixed ( 2 ) ) ;
159
+
160
+ }
39
161
40
162
// Iteration #5: Unique arrays
41
163
const wordsUnique = [
@@ -52,15 +174,40 @@ const wordsUnique = [
52
174
'bring'
53
175
] ;
54
176
55
- function uniquifyArray ( ) { }
177
+ function uniquifyArray ( words ) {
178
+
179
+ if ( words . length === 0 ) {
180
+ return null
181
+ }
182
+
183
+ let unique = [ ] ;
184
+ words . forEach ( function ( word ) {
185
+ if ( ! unique . includes ( word ) ) {
186
+ unique . push ( word ) ;
187
+ }
188
+ } ) ;
189
+ return unique ;
190
+ }
191
+
56
192
57
193
58
194
59
195
// Iteration #6: Find elements
60
196
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
197
62
- function doesWordExist ( ) { }
63
-
198
+ function doesWordExist ( wordsFind , wordSearch ) {
199
+ if ( wordsFind . length === 0 ) {
200
+ return null ;
201
+ }
202
+
203
+ for ( let i = 0 ; i < wordsFind . length ; i ++ ) {
204
+ found = [ ] ;
205
+ if ( wordSearch . includes ( wordsFind [ i ] ) ) {
206
+ return true ;
207
+ }
208
+ }
209
+ return found . includes ( wordsFind [ wordSearch ] ) ;
210
+ }
64
211
65
212
66
213
// Iteration #7: Count repetition
@@ -78,7 +225,25 @@ const wordsCount = [
78
225
'matter'
79
226
] ;
80
227
81
- function howManyTimes ( ) { }
228
+ function howManyTimes ( words , wordSearch ) {
229
+ if ( words . length === 0 ) {
230
+ return 0 ;
231
+ }
232
+
233
+ //other solution with reduce
234
+ // return words.reduce((acc, word) => {
235
+ // return (wordSearch === word ? acc + 1 : acc)
236
+ // }, 0);
237
+ //};
238
+
239
+ let wordTimes = 0 ;
240
+ words . flat ( Infinity ) . forEach ( word => {
241
+ if ( word === wordSearch ) {
242
+ wordTimes ++
243
+ }
244
+ } )
245
+ return wordTimes
246
+ }
82
247
83
248
84
249
@@ -106,8 +271,9 @@ const matrix = [
106
271
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
107
272
] ;
108
273
109
- function greatestProduct ( ) { }
274
+ function greatestProduct ( arr ) {
110
275
276
+ }
111
277
112
278
113
279
0 commit comments