1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
2
+ function maxOfTwoNumbers ( num1 , num2 ) {
3
+ if ( num1 > num2 ) {
4
+ return num1 ;
5
+ } else {
6
+ return num2 ;
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 ( words ) {
16
+ if ( words . length === 0 ) {
17
+ return null ;
18
+ }
19
+ let longestWord = '' ;
20
+ for ( let i = 0 ; i < words . length ; i ++ ) {
21
+ if ( words [ i ] . length > longestWord . length ) {
22
+ longestWord = words [ i ] ;
23
+ }
24
+ }
25
+ return longestWord ;
26
+ }
10
27
11
28
12
29
13
30
// Iteration #3: Calculate the sum
14
31
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
32
16
- function sumNumbers ( ) { }
33
+ function sumNumbers ( numbers ) {
34
+ if ( numbers . length === 0 ) {
35
+ return 0 ;
36
+ }
37
+ let sum = 0 ;
38
+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
39
+ sum += numbers [ i ]
40
+ }
41
+ return sum ;
42
+ }
43
+
44
+ console . log ( sumNumbers ( numbers ) ) ;
17
45
18
46
19
47
20
- // Iteration #3.1 Bonus:
21
- function sum ( ) { }
48
+ // Iteration #3.2 Bonus:
49
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
50
+
51
+ function checkAllZero ( mixedArr ) {
52
+ for ( let i = 0 ; i < mixedArr . length ; i ++ ) {
53
+ if ( mixedArr [ i ] !== 0 ) {
54
+ return false ;
55
+ }
56
+ }
57
+ return true ;
58
+ }
59
+
60
+ function sum ( mixedArr ) {
61
+ if ( mixedArr . length === 0 ) {
62
+ return 0 ;
63
+ }
64
+ if ( checkAllZero ( mixedArr ) ) {
65
+ return 0 ;
66
+ }
67
+ let sum = 0 ;
68
+ for ( let i = 0 ; i < mixedArr . length ; i ++ ) {
69
+ if ( typeof mixedArr [ i ] === "number" ) {
70
+ sum += mixedArr [ i ] ;
71
+ }
72
+ else if ( typeof mixedArr [ i ] === "string" ) {
73
+ sum += mixedArr [ i ] . length ;
74
+ }
75
+ else if ( typeof mixedArr [ i ] === "boolean" ) {
76
+ if ( mixedArr [ i ] === true ) {
77
+ sum += 1 ;
78
+ } else {
79
+ sum += 0 ;
80
+ }
81
+ } else {
82
+ throw new Error ( "Unsupported data type" ) ;
83
+ }
84
+ }
85
+ return sum ;
86
+ }
22
87
23
88
24
89
25
90
// Iteration #4: Calculate the average
26
91
// Level 1: Array of numbers
27
92
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
93
29
- function averageNumbers ( ) { }
94
+ function averageNumbers ( numbersAvg ) {
95
+ if ( numbersAvg . length === 0 ) {
96
+ return null ;
97
+ }
98
+ let sum = 0 ;
99
+ for ( let i = 0 ; i < numbersAvg . length ; i ++ ) {
100
+ sum += numbersAvg [ i ]
101
+ }
102
+ return sum / numbersAvg . length ;
103
+ }
104
+
105
+ console . log ( averageNumbers ( numbersAvg ) ) ;
30
106
31
107
32
108
// Level 2: Array of strings
33
109
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
110
35
- function averageWordLength ( ) { }
111
+ function averageWordLength ( wordsArr ) {
112
+ if ( wordsArr . length === 0 ) {
113
+ return null ;
114
+ }
115
+
116
+ let wordLength = 0 ;
117
+ let avgwordLength = 0 ;
118
+ for ( let i = 0 ; i < wordsArr . length ; i ++ ) {
119
+ wordLength = wordsArr [ i ] . length ;
120
+ avgwordLength += wordLength ;
121
+ }
122
+ return avgwordLength / wordsArr . length ;
123
+ }
124
+
125
+
126
+ // Bonus - Iteration #4.3
127
+ const arr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
128
+ function avg ( arr ) {
129
+ if ( arr . length === 0 ) {
130
+ return null ;
131
+ }
132
+ let sum = 0 ;
133
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
134
+ if ( typeof arr [ i ] === "number" ) {
135
+ sum += arr [ i ] ;
136
+ }
137
+ else if ( typeof arr [ i ] === "string" ) {
138
+ sum += arr [ i ] . length ;
139
+ }
140
+ else if ( typeof arr [ i ] === "boolean" ) {
141
+ if ( arr [ i ] === true ) {
142
+ sum += 1 ;
143
+ } else {
144
+ sum += 0 ;
145
+ }
146
+ } else {
147
+ throw new Error ( "Unsupported data type" ) ;
148
+ }
149
+ }
150
+ return sum / arr . length ;
151
+ }
152
+
153
+ console . log ( avg ( arr ) ) ;
154
+
36
155
37
- // Bonus - Iteration #4.1
38
- function avg ( ) { }
39
156
40
157
// Iteration #5: Unique arrays
41
158
const wordsUnique = [
@@ -52,14 +169,41 @@ const wordsUnique = [
52
169
'bring'
53
170
] ;
54
171
55
- function uniquifyArray ( ) { }
172
+ function uniquifyArray ( wordsUnique ) {
173
+ if ( wordsUnique . length === 0 ) {
174
+ return null ;
175
+ }
176
+ let uniqueArray = [ ] ;
177
+ for ( let i = 0 ; i < wordsUnique . length ; i ++ ) {
178
+ if ( ! uniqueArray . includes ( wordsUnique [ i ] ) ) {
179
+ uniqueArray . push ( wordsUnique [ i ] ) ;
180
+ }
181
+ }
182
+ return uniqueArray ;
183
+ }
56
184
57
185
58
186
59
187
// Iteration #6: Find elements
60
188
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
189
62
- function doesWordExist ( ) { }
190
+ function doesWordExist ( wordsFind , wordToFind ) {
191
+ if ( wordsFind . length === 0 ) {
192
+ return null ;
193
+ }
194
+
195
+ let isFounded = false ;
196
+
197
+ for ( let i = 0 ; i < wordsFind . length ; i ++ ) {
198
+ if ( wordsFind [ i ] === wordToFind ) {
199
+ isFounded = true ;
200
+ }
201
+ }
202
+
203
+ return isFounded ;
204
+ }
205
+
206
+ console . log ( doesWordExist ( wordsFind , 'machine' ) ) ;
63
207
64
208
65
209
@@ -78,8 +222,20 @@ const wordsCount = [
78
222
'matter'
79
223
] ;
80
224
81
- function howManyTimes ( ) { }
225
+ function howManyTimes ( wordsCount , wordToSearch ) {
226
+ if ( wordsCount . length === 0 ) {
227
+ return 0 ;
228
+ }
229
+ let count = 0 ;
230
+ for ( let i = 0 ; i < wordsCount . length ; i ++ ) {
231
+ if ( wordToSearch === wordsCount [ i ] ) {
232
+ count ++ ;
233
+ }
234
+ }
235
+ return count ;
236
+ }
82
237
238
+ console . log ( doesWordExist ( wordsFind , 'matter' ) ) ;
83
239
84
240
85
241
// Iteration #8: Bonus
@@ -106,7 +262,7 @@ const matrix = [
106
262
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
107
263
] ;
108
264
109
- function greatestProduct ( ) { }
265
+ function greatestProduct ( ) { }
110
266
111
267
112
268
@@ -128,3 +284,5 @@ if (typeof module !== 'undefined') {
128
284
greatestProduct
129
285
} ;
130
286
}
287
+
288
+
0 commit comments