1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
2
+ function maxOfTwoNumbers ( a , b ) {
3
+ if ( a > b ) return a ;
4
+ else if ( a < b ) return b ;
5
+ else return a ;
6
+ }
5
7
6
8
// Iteration #2: Find longest word
7
- const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
-
9
- function findLongestWord ( ) { }
10
-
9
+ const words = [
10
+ "mystery" ,
11
+ "brother" ,
12
+ "aviator" ,
13
+ "crocodile" ,
14
+ "pearl" ,
15
+ "orchard" ,
16
+ "crackpot" ,
17
+ ] ;
11
18
19
+ function findLongestWord ( words ) {
20
+ let indexLongest = 0 ;
21
+ let longestWords = [ ] ;
22
+ if ( words . length === 0 ) {
23
+ return null ;
24
+ }
25
+ for ( let i = 0 ; i < words . length ; i ++ ) {
26
+ if ( words [ i ] . length > words [ indexLongest ] . length ) {
27
+ indexLongest = i ;
28
+ longestWords = [ words [ i ] ] ;
29
+ } else if ( words [ i ] . length === words [ indexLongest ] . length ) {
30
+ longestWords . push ( words [ i ] ) ;
31
+ }
32
+ }
33
+ return words [ indexLongest ] ;
34
+ }
12
35
13
36
// Iteration #3: Calculate the sum
14
37
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
38
16
- function sumNumbers ( ) { }
17
-
18
-
39
+ function sumNumbers ( numbers ) {
40
+ if ( numbers . length === 0 ) {
41
+ return 0 ;
42
+ }
43
+ let totalSum = 0 ;
44
+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
45
+ totalSum = totalSum + numbers [ i ] ;
46
+ }
47
+ return totalSum ;
48
+ }
19
49
20
50
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
22
-
23
-
51
+ function sum ( numbers ) {
52
+ if ( numbers . length === 0 ) {
53
+ return 0 ;
54
+ }
55
+ let totalSum = 0 ;
56
+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
57
+ if ( typeof numbers [ i ] === "number" ) {
58
+ totalSum = totalSum + numbers [ i ] ;
59
+ } else if ( typeof numbers [ i ] === "string" ) {
60
+ totalSum = totalSum + numbers [ i ] . length ;
61
+ } else if ( typeof numbers [ i ] === "boolean" ) {
62
+ if ( numbers [ i ] == true ) {
63
+ totalSum = totalSum + 1 ;
64
+ }
65
+ } else {
66
+ throw new Error ( `Unsupported data` ) ;
67
+ }
68
+ }
69
+ return totalSum ;
70
+ }
24
71
25
72
// Iteration #4: Calculate the average
26
73
// Level 1: Array of numbers
27
74
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
75
29
- function averageNumbers ( ) { }
30
-
76
+ function averageNumbers ( numbersAvg ) {
77
+ if ( numbersAvg . length === 0 ) {
78
+ return null ;
79
+ }
80
+ let totalSum = 0 ;
81
+ for ( let i = 0 ; i < numbersAvg . length ; i ++ ) {
82
+ totalSum = totalSum + numbersAvg [ i ] ;
83
+ }
84
+ return totalSum / numbersAvg . length ;
85
+ }
31
86
32
87
// Level 2: Array of strings
33
- const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
88
+ const wordsArr = [
89
+ "seat" ,
90
+ "correspond" ,
91
+ "linen" ,
92
+ "motif" ,
93
+ "hole" ,
94
+ "smell" ,
95
+ "smart" ,
96
+ "chaos" ,
97
+ "fuel" ,
98
+ "palace" ,
99
+ ] ;
34
100
35
- function averageWordLength ( ) { }
101
+ function averageWordLength ( wordsArr ) {
102
+ if ( wordsArr . length === 0 ) {
103
+ return null ;
104
+ }
105
+ let totalLength = 0 ;
106
+ for ( let i = 0 ; i < wordsArr . length ; i ++ ) {
107
+ totalLength = totalLength + wordsArr [ i ] . length ;
108
+ }
109
+ return totalLength / wordsArr . length ;
110
+ }
36
111
37
112
// Bonus - Iteration #4.1
38
- function avg ( ) { }
113
+ const mixedArr = [ 6 , 12 , "miami" , 1 , true , "barca" , "200" , "lisboa" , 8 , 10 ] ;
114
+ function avg ( mixedArr ) {
115
+ if ( ! mixedArr . length ) {
116
+ return null ;
117
+ }
118
+ let totalLength = 0 ;
119
+ for ( let i = 0 ; i < mixedArr . length ; i ++ ) {
120
+ if ( typeof mixedArr [ i ] === "string" ) {
121
+ totalLength = totalLength + mixedArr [ i ] . length ;
122
+ } else if ( typeof mixedArr [ i ] === "number" ) {
123
+ totalLength = totalLength + mixedArr [ i ] ;
124
+ } else if ( typeof mixedArr [ i ] === "boolean" ) {
125
+ if ( mixedArr [ i ] === true ) {
126
+ totalLength = totalLength + 1 ;
127
+ }
128
+ }
129
+ }
130
+ return totalLength / mixedArr . length ;
131
+ }
39
132
40
133
// Iteration #5: Unique arrays
41
134
const wordsUnique = [
42
- ' crab' ,
43
- ' poison' ,
44
- ' contagious' ,
45
- ' simple' ,
46
- ' bring' ,
47
- ' sharp' ,
48
- ' playground' ,
49
- ' poison' ,
50
- ' communion' ,
51
- ' simple' ,
52
- ' bring'
135
+ " crab" ,
136
+ " poison" ,
137
+ " contagious" ,
138
+ " simple" ,
139
+ " bring" ,
140
+ " sharp" ,
141
+ " playground" ,
142
+ " poison" ,
143
+ " communion" ,
144
+ " simple" ,
145
+ " bring" ,
53
146
] ;
54
147
55
- function uniquifyArray ( ) { }
56
-
57
-
148
+ function uniquifyArray ( wordsUnique ) {
149
+ let wordsUniqueNoDupl = [ ] ;
150
+ if ( ! wordsUnique . length ) {
151
+ return null ;
152
+ }
153
+ for ( let i = 0 ; i < wordsUnique . length ; i ++ ) {
154
+ if ( wordsUniqueNoDupl . indexOf ( wordsUnique [ i ] ) === - 1 ) {
155
+ wordsUniqueNoDupl . push ( wordsUnique [ i ] ) ;
156
+ }
157
+ }
158
+ return wordsUniqueNoDupl ;
159
+ console . log ( wordsUniqueNoDupl ) ;
160
+ //console.log(wordsUnique.indexOf("crab"));
161
+ }
58
162
59
163
// Iteration #6: Find elements
60
- const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
-
62
- function doesWordExist ( ) { }
63
-
164
+ const wordsFind = [
165
+ "machine" ,
166
+ "subset" ,
167
+ "trouble" ,
168
+ "starting" ,
169
+ "matter" ,
170
+ "eating" ,
171
+ "truth" ,
172
+ "disobedience" ,
173
+ ] ;
64
174
175
+ function doesWordExist ( wordlist , wordToSearch ) {
176
+ if ( ! wordlist . length ) { return null ; }
177
+ let exists = false ;
178
+ for ( let i = 0 ; i < wordlist . length ; i ++ ) {
179
+ if ( wordlist [ i ] === wordToSearch ) { exists = true ; }
180
+ }
181
+ return exists ;
182
+ }
65
183
66
184
// Iteration #7: Count repetition
67
185
const wordsCount = [
68
- ' machine' ,
69
- ' matter' ,
70
- ' subset' ,
71
- ' trouble' ,
72
- ' starting' ,
73
- ' matter' ,
74
- ' eating' ,
75
- ' matter' ,
76
- ' truth' ,
77
- ' disobedience' ,
78
- ' matter'
186
+ " machine" ,
187
+ " matter" ,
188
+ " subset" ,
189
+ " trouble" ,
190
+ " starting" ,
191
+ " matter" ,
192
+ " eating" ,
193
+ " matter" ,
194
+ " truth" ,
195
+ " disobedience" ,
196
+ " matter" ,
79
197
] ;
80
198
81
- function howManyTimes ( ) { }
82
-
83
-
199
+ function howManyTimes ( wordlist , wordToSearch ) {
200
+ if ( ! wordlist . length ) { return 0 ; }
201
+ let totalTimes = 0 ;
202
+ for ( let i = 0 ; i < wordlist . length ; i ++ ) {
203
+ if ( wordlist [ i ] === wordToSearch ) { totalTimes = totalTimes + 1 ; }
204
+ }
205
+ return totalTimes ;
206
+ }
84
207
85
208
// Iteration #8: Bonus
86
209
const matrix = [
87
210
[ 8 , 2 , 22 , 97 , 38 , 15 , 0 , 40 , 0 , 75 , 4 , 5 , 7 , 78 , 52 , 12 , 50 , 77 , 91 , 8 ] ,
88
- [ 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 4 , 56 , 62 , 0 ] ,
89
- [ 81 , 49 , 31 , 73 , 55 , 79 , 14 , 29 , 93 , 71 , 40 , 67 , 53 , 88 , 30 , 3 , 49 , 13 , 36 , 65 ] ,
211
+ [
212
+ 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 4 , 56 , 62 ,
213
+ 0 ,
214
+ ] ,
215
+ [
216
+ 81 , 49 , 31 , 73 , 55 , 79 , 14 , 29 , 93 , 71 , 40 , 67 , 53 , 88 , 30 , 3 , 49 , 13 , 36 ,
217
+ 65 ,
218
+ ] ,
90
219
[ 52 , 70 , 95 , 23 , 4 , 60 , 11 , 42 , 69 , 24 , 68 , 56 , 1 , 32 , 56 , 71 , 37 , 2 , 36 , 91 ] ,
91
- [ 22 , 31 , 16 , 71 , 51 , 67 , 63 , 89 , 41 , 92 , 36 , 54 , 22 , 40 , 40 , 28 , 66 , 33 , 13 , 80 ] ,
92
- [ 24 , 47 , 32 , 60 , 99 , 3 , 45 , 2 , 44 , 75 , 33 , 53 , 78 , 36 , 84 , 20 , 35 , 17 , 12 , 50 ] ,
93
- [ 32 , 98 , 81 , 28 , 64 , 23 , 67 , 10 , 26 , 38 , 40 , 67 , 59 , 54 , 70 , 66 , 18 , 38 , 64 , 70 ] ,
94
- [ 67 , 26 , 20 , 68 , 2 , 62 , 12 , 20 , 95 , 63 , 94 , 39 , 63 , 8 , 40 , 91 , 66 , 49 , 94 , 21 ] ,
95
- [ 24 , 55 , 58 , 5 , 66 , 73 , 99 , 26 , 97 , 17 , 78 , 78 , 96 , 83 , 14 , 88 , 34 , 89 , 63 , 72 ] ,
220
+ [
221
+ 22 , 31 , 16 , 71 , 51 , 67 , 63 , 89 , 41 , 92 , 36 , 54 , 22 , 40 , 40 , 28 , 66 , 33 , 13 ,
222
+ 80 ,
223
+ ] ,
224
+ [
225
+ 24 , 47 , 32 , 60 , 99 , 3 , 45 , 2 , 44 , 75 , 33 , 53 , 78 , 36 , 84 , 20 , 35 , 17 , 12 ,
226
+ 50 ,
227
+ ] ,
228
+ [
229
+ 32 , 98 , 81 , 28 , 64 , 23 , 67 , 10 , 26 , 38 , 40 , 67 , 59 , 54 , 70 , 66 , 18 , 38 , 64 ,
230
+ 70 ,
231
+ ] ,
232
+ [
233
+ 67 , 26 , 20 , 68 , 2 , 62 , 12 , 20 , 95 , 63 , 94 , 39 , 63 , 8 , 40 , 91 , 66 , 49 , 94 ,
234
+ 21 ,
235
+ ] ,
236
+ [
237
+ 24 , 55 , 58 , 5 , 66 , 73 , 99 , 26 , 97 , 17 , 78 , 78 , 96 , 83 , 14 , 88 , 34 , 89 , 63 ,
238
+ 72 ,
239
+ ] ,
96
240
[ 21 , 36 , 23 , 9 , 75 , 0 , 76 , 44 , 20 , 45 , 35 , 14 , 0 , 61 , 33 , 97 , 34 , 31 , 33 , 95 ] ,
97
241
[ 78 , 17 , 53 , 28 , 22 , 75 , 31 , 67 , 15 , 94 , 3 , 80 , 4 , 62 , 16 , 14 , 9 , 53 , 56 , 92 ] ,
98
- [ 16 , 39 , 5 , 42 , 96 , 35 , 31 , 47 , 55 , 58 , 88 , 24 , 0 , 17 , 54 , 24 , 36 , 29 , 85 , 57 ] ,
242
+ [
243
+ 16 , 39 , 5 , 42 , 96 , 35 , 31 , 47 , 55 , 58 , 88 , 24 , 0 , 17 , 54 , 24 , 36 , 29 , 85 ,
244
+ 57 ,
245
+ ] ,
99
246
[ 86 , 56 , 0 , 48 , 35 , 71 , 89 , 7 , 5 , 44 , 44 , 37 , 44 , 60 , 21 , 58 , 51 , 54 , 17 , 58 ] ,
100
- [ 19 , 80 , 81 , 68 , 5 , 94 , 47 , 69 , 28 , 73 , 92 , 13 , 86 , 52 , 17 , 77 , 4 , 89 , 55 , 40 ] ,
247
+ [
248
+ 19 , 80 , 81 , 68 , 5 , 94 , 47 , 69 , 28 , 73 , 92 , 13 , 86 , 52 , 17 , 77 , 4 , 89 , 55 ,
249
+ 40 ,
250
+ ] ,
101
251
[ 4 , 52 , 8 , 83 , 97 , 35 , 99 , 16 , 7 , 97 , 57 , 32 , 16 , 26 , 26 , 79 , 33 , 27 , 98 , 66 ] ,
102
- [ 88 , 36 , 68 , 87 , 57 , 62 , 20 , 72 , 3 , 46 , 33 , 67 , 46 , 55 , 12 , 32 , 63 , 93 , 53 , 69 ] ,
103
- [ 4 , 42 , 16 , 73 , 38 , 25 , 39 , 11 , 24 , 94 , 72 , 18 , 8 , 46 , 29 , 32 , 40 , 62 , 76 , 36 ] ,
104
- [ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 , 16 ] ,
105
- [ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 , 54 ] ,
106
- [ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
252
+ [
253
+ 88 , 36 , 68 , 87 , 57 , 62 , 20 , 72 , 3 , 46 , 33 , 67 , 46 , 55 , 12 , 32 , 63 , 93 , 53 ,
254
+ 69 ,
255
+ ] ,
256
+ [
257
+ 4 , 42 , 16 , 73 , 38 , 25 , 39 , 11 , 24 , 94 , 72 , 18 , 8 , 46 , 29 , 32 , 40 , 62 , 76 ,
258
+ 36 ,
259
+ ] ,
260
+ [
261
+ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 ,
262
+ 16 ,
263
+ ] ,
264
+ [
265
+ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 ,
266
+ 54 ,
267
+ ] ,
268
+ [
269
+ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 ,
270
+ 48 ,
271
+ ] ,
107
272
] ;
108
273
109
- function greatestProduct ( ) { }
110
-
111
-
112
-
274
+ function greatestProduct ( matrix ) { }
113
275
114
276
// The following is required to make unit tests work.
115
277
/* Environment setup. Do not modify the below code. */
116
- if ( typeof module !== ' undefined' ) {
278
+ if ( typeof module !== " undefined" ) {
117
279
module . exports = {
118
280
maxOfTwoNumbers,
119
281
findLongestWord,
@@ -125,6 +287,6 @@ if (typeof module !== 'undefined') {
125
287
uniquifyArray,
126
288
doesWordExist,
127
289
howManyTimes,
128
- greatestProduct
290
+ greatestProduct,
129
291
} ;
130
292
}
0 commit comments