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