1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
2
+ function maxOfTwoNumbers ( number1 , number2 ) {
3
+ if ( number1 >= number2 ) {
4
+ return number1
5
+ } else {
6
+ return number2
7
+ }
8
+ }
5
9
6
10
// Iteration #2: Find longest word
7
- const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
-
9
- function findLongestWord ( ) { }
10
-
11
-
11
+ const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ]
12
+
13
+ function findLongestWord ( wordsArray ) {
14
+ if ( wordsArray . length == 0 ) {
15
+ return null
16
+ }
17
+ if ( wordsArray . length == 1 ) {
18
+ return wordsArray [ 0 ]
19
+ }
20
+ let longestWord = ''
21
+ let longestLength = 0
22
+ for ( let i = 0 ; i < wordsArray . length ; i ++ ) {
23
+ if ( wordsArray [ i ] . length > longestLength ) {
24
+ longestLength = wordsArray [ i ] . length
25
+ longestWord = wordsArray [ i ]
26
+ }
27
+ }
28
+ return longestWord
29
+ }
12
30
13
31
// Iteration #3: Calculate the sum
14
- const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
-
16
- function sumNumbers ( ) { }
17
-
18
-
32
+ const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ]
33
+
34
+ function sumNumbers ( numbersArray ) {
35
+ if ( numbersArray . length == 0 ) {
36
+ return 0
37
+ }
38
+ if ( numbersArray . length == 1 ) {
39
+ return numbersArray [ 0 ]
40
+ }
41
+ let partialSum = 0
42
+ for ( let i = 0 ; i < numbersArray . length ; i ++ ) {
43
+ partialSum += numbersArray [ i ]
44
+ }
45
+ return partialSum
46
+ }
19
47
20
48
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
22
-
23
-
49
+ function sum ( genericArray ) {
50
+ if ( genericArray . length == 0 ) {
51
+ return 0
52
+ }
53
+ if ( genericArray . length == 1 ) {
54
+ return genericArray [ 0 ]
55
+ }
56
+ let partialSum = 0
57
+ for ( let i = 0 ; i < genericArray . length ; i ++ ) {
58
+ if ( typeof genericArray [ i ] === 'string' ) {
59
+ partialSum += genericArray [ i ] . length
60
+ } else if ( typeof genericArray [ i ] === 'boolean' ) {
61
+ if ( genericArray [ i ] === true ) {
62
+ partialSum ++
63
+ }
64
+ } else if ( typeof genericArray [ i ] === 'object' || typeof genericArray [ i ] === 'array' ) {
65
+ throw "Unsupported data type sir or ma'am"
66
+ } else {
67
+ partialSum += genericArray [ i ]
68
+ }
69
+ }
70
+ return partialSum
71
+ }
24
72
25
73
// Iteration #4: Calculate the average
26
74
// Level 1: Array of numbers
27
- const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
-
29
- function averageNumbers ( ) { }
30
-
75
+ const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ]
76
+
77
+ function averageNumbers ( numbersArray ) {
78
+ if ( numbersArray . length == 0 ) {
79
+ return null
80
+ }
81
+ if ( numbersArray . length == 1 ) {
82
+ return numbersArray [ 0 ]
83
+ }
84
+ return sumNumbers ( numbersArray ) / numbersArray . length
85
+ }
31
86
32
87
// Level 2: Array of strings
33
- const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
-
35
- function averageWordLength ( ) { }
88
+ const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ]
89
+
90
+ function averageWordLength ( wordsArray ) {
91
+ if ( wordsArray . length == 0 ) {
92
+ return null
93
+ }
94
+ let partialSum = 0
95
+ for ( let i = 0 ; i < wordsArray . length ; i ++ ) {
96
+ partialSum += wordsArray [ i ] . length
97
+ }
98
+ return partialSum / wordsArray . length
99
+ }
36
100
37
101
// Bonus - Iteration #4.1
38
- function avg ( ) { }
102
+ function avg ( mixedArray ) {
103
+ if ( mixedArray . length == 0 ) {
104
+ return null
105
+ }
106
+ return Math . round ( ( sum ( mixedArray ) / mixedArray . length ) * 100 ) / 100
107
+ }
39
108
40
109
// Iteration #5: Unique arrays
41
110
const wordsUnique = [
@@ -50,18 +119,37 @@ const wordsUnique = [
50
119
'communion' ,
51
120
'simple' ,
52
121
'bring'
53
- ] ;
54
-
55
- function uniquifyArray ( ) { }
56
-
57
-
122
+ ]
123
+
124
+ function uniquifyArray ( arrayToBeUniquified ) {
125
+ if ( arrayToBeUniquified . length == 0 ) {
126
+ return null
127
+ }
128
+ let uniquifiedArray = [ ]
129
+ for ( let i = 0 ; i < arrayToBeUniquified . length ; i ++ ) {
130
+ if ( uniquifiedArray . includes ( arrayToBeUniquified [ i ] ) ) {
131
+ continue
132
+ } else {
133
+ uniquifiedArray . push ( arrayToBeUniquified [ i ] )
134
+ }
135
+ }
136
+ return uniquifiedArray
137
+ }
58
138
59
139
// Iteration #6: Find elements
60
- const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
-
62
- function doesWordExist ( ) { }
63
-
64
-
140
+ const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ]
141
+
142
+ function doesWordExist ( wordsArray , wordToSearch ) {
143
+ if ( wordsArray . length == 0 ) {
144
+ return null
145
+ }
146
+ for ( let i = 0 ; i < wordsArray . length ; i ++ ) {
147
+ if ( wordsArray [ i ] === wordToSearch ) {
148
+ return true
149
+ }
150
+ }
151
+ return false
152
+ }
65
153
66
154
// Iteration #7: Count repetition
67
155
const wordsCount = [
@@ -76,11 +164,20 @@ const wordsCount = [
76
164
'truth' ,
77
165
'disobedience' ,
78
166
'matter'
79
- ] ;
80
-
81
- function howManyTimes ( ) { }
82
-
83
-
167
+ ]
168
+
169
+ function howManyTimes ( wordsArray , wordToSearch ) {
170
+ if ( wordsArray . length == 0 ) {
171
+ return 0
172
+ }
173
+ let countTimes = 0
174
+ for ( let i = 0 ; i < wordsArray . length ; i ++ ) {
175
+ if ( wordsArray [ i ] === wordToSearch ) {
176
+ countTimes ++
177
+ }
178
+ }
179
+ return countTimes
180
+ }
84
181
85
182
// Iteration #8: Bonus
86
183
const matrix = [
@@ -104,12 +201,66 @@ const matrix = [
104
201
[ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 , 16 ] ,
105
202
[ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 , 54 ] ,
106
203
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
107
- ] ;
204
+ ]
108
205
109
- function greatestProduct ( ) { }
206
+ function greatestLineProduct ( arrayToTest ) {
207
+ let greatestPartialLineProduct = 0
208
+ let partialLineProduct = 0
209
+ for ( let i = 0 ; i < arrayToTest . length - 3 ; i ++ ) {
210
+ partialLineProduct = arrayToTest [ i ] * arrayToTest [ i + 1 ] * arrayToTest [ i + 2 ] * arrayToTest [ i + 3 ]
110
211
212
+ if ( partialLineProduct > greatestPartialLineProduct ) {
213
+ greatestPartialLineProduct = partialLineProduct
214
+ }
215
+ }
216
+
217
+ return greatestPartialLineProduct
218
+ }
111
219
220
+ function transposeMatrix ( matrixToTranspose ) {
221
+ const transposedMatrix = [ ]
222
+
223
+ for ( let i = 0 ; i < matrixToTranspose [ 0 ] . length ; i ++ ) {
224
+ transposedMatrix . push ( [ ] )
225
+ for ( let j = 0 ; j < matrixToTranspose . length ; j ++ ) {
226
+ transposedMatrix [ i ] . push ( matrixToTranspose [ j ] [ i ] )
227
+ }
228
+ }
229
+ return transposedMatrix
230
+ }
231
+
232
+ const tm = [
233
+ [ 1 , 2 , 3 , 4 , 5 ] ,
234
+ [ 1 , 20 , 3 , 4 , 5 ] ,
235
+ [ 1 , 20 , 3 , 4 , 5 ] ,
236
+ [ 1 , 20 , 3 , 4 , 5 ] ,
237
+ [ 1 , 4 , 3 , 4 , 5 ]
238
+ ]
239
+
240
+ function greatestProduct ( matrixToTest ) {
241
+ let greatestGreatestLineProduct = 0
242
+ for ( let i = 0 ; i < matrixToTest . length ; i ++ ) {
243
+ if ( greatestLineProduct ( matrixToTest [ i ] ) > greatestGreatestLineProduct ) {
244
+ greatestGreatestLineProduct = greatestLineProduct ( matrixToTest [ i ] )
245
+ }
246
+ }
247
+ console . log ( 'greatestGreatestLineProduct' , greatestGreatestLineProduct )
248
+ let greatestGreatestColumnProduct = 0
249
+ const transposedMatrix = transposeMatrix ( matrixToTest )
250
+ for ( let j = 0 ; j < transposedMatrix . length ; j ++ ) {
251
+ if ( greatestLineProduct ( transposedMatrix [ j ] ) > greatestGreatestColumnProduct ) {
252
+ greatestGreatestColumnProduct = greatestLineProduct ( transposedMatrix [ j ] )
253
+ }
254
+ }
255
+ console . log ( 'greatestGreatestColumnProduct' , greatestGreatestColumnProduct )
256
+ if ( greatestGreatestLineProduct >= greatestGreatestColumnProduct ) {
257
+ return greatestGreatestLineProduct
258
+ } else {
259
+ return greatestGreatestColumnProduct
260
+ }
261
+ }
112
262
263
+ console . log ( greatestProduct ( matrix ) )
113
264
114
265
// The following is required to make unit tests work.
115
266
/* Environment setup. Do not modify the below code. */
@@ -126,5 +277,5 @@ if (typeof module !== 'undefined') {
126
277
doesWordExist,
127
278
howManyTimes,
128
279
greatestProduct
129
- } ;
280
+ }
130
281
}
0 commit comments