1
1
// Find the maximum
2
2
function maxOfTwoNumbers ( first , second ) {
3
-
3
+ if ( first >= second ) {
4
+ return first ;
5
+ }
6
+ else return second ;
4
7
}
5
8
6
9
var largest = maxOfTwoNumbers ( 2 , 6 ) ;
7
10
console . log ( largest ) ;
8
11
9
12
// Finding Longest Word
10
13
function findLongestWord ( words ) {
11
-
14
+ var longestword = words [ 0 ] ;
15
+ for ( var i = 1 ; i < words . length ; i ++ ) {
16
+ if ( words [ i ] . length > longestword . length ) {
17
+ longestword = words [ i ] ;
18
+ }
19
+ }
20
+ return longestword ;
12
21
}
13
22
14
23
var words = [
@@ -25,7 +34,11 @@ console.log(longest);
25
34
26
35
// Calculating a Sum
27
36
function sumArray ( array ) {
28
-
37
+ var sum = 0 ;
38
+ for ( var i = 0 ; i < array . length ; i ++ ) {
39
+ sum += array [ i ] ;
40
+ }
41
+ return sum ;
29
42
}
30
43
31
44
var numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
@@ -34,7 +47,7 @@ console.log(total);
34
47
35
48
// Calculate the Average
36
49
function averageNumbers ( array ) {
37
-
50
+ return sumArray ( array ) / array . length ;
38
51
}
39
52
40
53
var numbers = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
@@ -43,7 +56,11 @@ console.log(average);
43
56
44
57
// Array of Strings
45
58
function averageWordLength ( array ) {
46
-
59
+ var lengthSum = 0 ;
60
+ for ( var i = 0 ; i < array . length ; i ++ ) {
61
+ lengthSum += array [ i ] . length ;
62
+ }
63
+ return lengthSum / array . length ;
47
64
}
48
65
49
66
var words = [
@@ -63,7 +80,13 @@ console.log(averageLength);
63
80
64
81
// Unique Arrays
65
82
function uniquifyArray ( array ) {
66
-
83
+ var uniqueArray = [ ] ;
84
+ for ( var i = 0 ; i < array . length ; i ++ ) {
85
+ if ( uniqueArray . indexOf ( array [ i ] ) == - 1 ) {
86
+ uniqueArray . push ( array [ i ] ) ;
87
+ }
88
+ }
89
+ return uniqueArray ;
67
90
}
68
91
69
92
var words = [
@@ -84,7 +107,12 @@ console.log(uniqued);
84
107
85
108
// Finding Elements
86
109
function doesWordExist ( wordsArray , word ) {
87
-
110
+ for ( var i = 0 ; i < wordsArray . length ; i ++ ) {
111
+ if ( word === wordsArray [ i ] ) {
112
+ return true ;
113
+ }
114
+ }
115
+ return false ;
88
116
}
89
117
90
118
var words = [
@@ -106,7 +134,13 @@ console.log(hasDog);
106
134
107
135
// Counting Repetion
108
136
function howManyTimes ( words , word ) {
109
-
137
+ var howMany = 0 ;
138
+ for ( var i = 0 ; i < words . length ; i ++ ) {
139
+ if ( word === words [ i ] ) {
140
+ howMany ++ ;
141
+ }
142
+ }
143
+ return howMany ;
110
144
}
111
145
112
146
var words = [
@@ -131,31 +165,66 @@ console.log(howManyDog);
131
165
132
166
// Bonus Quest
133
167
function greatestProduct ( matrix ) {
134
-
168
+ var productFinal = 0 ;
169
+ for ( var i = 0 ; i < matrix . length ; i ++ ) {
170
+ for ( j = 0 ; j < matrix [ i ] . length ; j ++ ) {
171
+ var product ;
172
+ if ( i === 0 ) {
173
+ if ( j === 0 ) {
174
+ product = matrix [ i ] [ j + 1 ] * matrix [ i + 1 ] [ j ] * matrix [ i ] [ matrix [ i ] . length - 1 ] * matrix [ matrix . length - 1 ] [ j ] ;
175
+ }
176
+ else if ( j === matrix [ i ] . length - 1 ) {
177
+ product = matrix [ i ] [ j ] * matrix [ i ] [ j - 1 ] * matrix [ i + 1 ] [ j ] * matrix [ matrix . length - 1 ] [ j ] ;
178
+
179
+ }
180
+ else {
181
+ product = matrix [ i ] [ j - 1 ] * matrix [ i ] [ j + 1 ] * matrix [ i + 1 ] [ j ] * matrix [ matrix . length - 1 ] [ j ] ;
182
+ }
183
+ } else if ( i === 19 ) {
184
+ if ( j === 0 ) {
185
+ product = matrix [ i - 1 ] [ j ] * matrix [ i ] [ j + 1 ] * matrix [ 0 ] [ 0 ] * matrix [ i ] [ matrix [ i ] . length - 1 ] ;
186
+ }
187
+ else if ( j === 19 ) {
188
+ product = matrix [ i ] [ j - 1 ] * matrix [ i - 1 ] [ matrix [ i - 1 ] . length - 1 ] * matrix [ 0 ] [ matrix [ 0 ] . length - 1 ] * matrix [ i ] [ 0 ] ;
189
+ }
190
+ else {
191
+ product = matrix [ i ] [ j - 1 ] * matrix [ i ] [ j + 1 ] * matrix [ i - 1 ] [ j ] * matrix [ 0 ] [ j ] ;
192
+ }
193
+ }
194
+ else {
195
+ product = matrix [ i ] [ j - 1 ] * matrix [ i ] [ j + 1 ] * matrix [ i + 1 ] [ j ] * matrix [ i - 1 ] [ j ] ;
196
+ }
197
+ if ( product > productFinal ) {
198
+ productFinal = product ;
199
+ }
200
+ }
201
+ }
202
+ return productFinal ;
135
203
}
136
204
137
205
var matrix = [
138
- [ 08 , 02 , 22 , 97 , 38 , 15 , 0 , 40 , 0 , 75 , 04 , 05 , 07 , 78 , 52 , 12 , 50 , 77 , 91 , 08 ] ,
139
- [ 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 04 , 56 , 62 , 0 ] ,
140
- [ 81 , 49 , 31 , 73 , 55 , 79 , 14 , 29 , 93 , 71 , 40 , 67 , 53 , 88 , 30 , 03 , 49 , 13 , 36 , 65 ] ,
141
- [ 52 , 70 , 95 , 23 , 04 , 60 , 11 , 42 , 69 , 24 , 68 , 56 , 01 , 32 , 56 , 71 , 37 , 02 , 36 , 91 ] ,
206
+ [ 8 , 2 , 22 , 97 , 38 , 15 , 0 , 40 , 0 , 75 , 4 , 5 , 7 , 78 , 52 , 12 , 50 , 77 , 91 , 8 ] ,
207
+ [ 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 4 , 56 , 62 , 0 ] ,
208
+ [ 81 , 49 , 31 , 73 , 55 , 79 , 14 , 29 , 93 , 71 , 40 , 67 , 53 , 88 , 30 , 3 , 49 , 13 , 36 , 65 ] ,
209
+ [ 52 , 70 , 95 , 23 , 4 , 60 , 11 , 42 , 69 , 24 , 68 , 56 , 1 , 32 , 56 , 71 , 37 , 2 , 36 , 91 ] ,
142
210
[ 22 , 31 , 16 , 71 , 51 , 67 , 63 , 89 , 41 , 92 , 36 , 54 , 22 , 40 , 40 , 28 , 66 , 33 , 13 , 80 ] ,
143
- [ 24 , 47 , 32 , 60 , 99 , 03 , 45 , 02 , 44 , 75 , 33 , 53 , 78 , 36 , 84 , 20 , 35 , 17 , 12 , 50 ] ,
211
+ [ 24 , 47 , 32 , 60 , 99 , 3 , 45 , 2 , 44 , 75 , 33 , 53 , 78 , 36 , 84 , 20 , 35 , 17 , 12 , 50 ] ,
144
212
[ 32 , 98 , 81 , 28 , 64 , 23 , 67 , 10 , 26 , 38 , 40 , 67 , 59 , 54 , 70 , 66 , 18 , 38 , 64 , 70 ] ,
145
- [ 67 , 26 , 20 , 68 , 02 , 62 , 12 , 20 , 95 , 63 , 94 , 39 , 63 , 08 , 40 , 91 , 66 , 49 , 94 , 21 ] ,
146
- [ 24 , 55 , 58 , 05 , 66 , 73 , 99 , 26 , 97 , 17 , 78 , 78 , 96 , 83 , 14 , 88 , 34 , 89 , 63 , 72 ] ,
147
- [ 21 , 36 , 23 , 09 , 75 , 0 , 76 , 44 , 20 , 45 , 35 , 14 , 0 , 61 , 33 , 97 , 34 , 31 , 33 , 95 ] ,
148
- [ 78 , 17 , 53 , 28 , 22 , 75 , 31 , 67 , 15 , 94 , 03 , 80 , 04 , 62 , 16 , 14 , 09 , 53 , 56 , 92 ] ,
149
- [ 16 , 39 , 05 , 42 , 96 , 35 , 31 , 47 , 55 , 58 , 88 , 24 , 0 , 17 , 54 , 24 , 36 , 29 , 85 , 57 ] ,
150
- [ 86 , 56 , 0 , 48 , 35 , 71 , 89 , 07 , 05 , 44 , 44 , 37 , 44 , 60 , 21 , 58 , 51 , 54 , 17 , 58 ] ,
151
- [ 19 , 80 , 81 , 68 , 05 , 94 , 47 , 69 , 28 , 73 , 92 , 13 , 86 , 52 , 17 , 77 , 04 , 89 , 55 , 40 ] ,
152
- [ 04 , 52 , 08 , 83 , 97 , 35 , 99 , 16 , 07 , 97 , 57 , 32 , 16 , 26 , 26 , 79 , 33 , 27 , 98 , 66 ] ,
213
+ [ 67 , 26 , 20 , 68 , 2 , 62 , 12 , 20 , 95 , 63 , 94 , 39 , 63 , 8 , 40 , 91 , 66 , 49 , 94 , 21 ] ,
214
+ [ 24 , 55 , 58 , 5 , 66 , 73 , 99 , 26 , 97 , 17 , 78 , 78 , 96 , 83 , 14 , 88 , 34 , 89 , 63 , 72 ] ,
215
+ [ 21 , 36 , 23 , 9 , 75 , 0 , 76 , 44 , 20 , 45 , 35 , 14 , 0 , 61 , 33 , 97 , 34 , 31 , 33 , 95 ] ,
216
+ [ 78 , 17 , 53 , 28 , 22 , 75 , 31 , 67 , 15 , 94 , 3 , 80 , 4 , 62 , 16 , 14 , 9 , 53 , 56 , 92 ] ,
217
+ [ 16 , 39 , 5 , 42 , 96 , 35 , 31 , 47 , 55 , 58 , 88 , 24 , 0 , 17 , 54 , 24 , 36 , 29 , 85 , 57 ] ,
218
+ [ 86 , 56 , 0 , 48 , 35 , 71 , 89 , 7 , 5 , 44 , 44 , 37 , 44 , 60 , 21 , 58 , 51 , 54 , 17 , 58 ] ,
219
+ [ 19 , 80 , 81 , 68 , 5 , 94 , 47 , 69 , 28 , 73 , 92 , 13 , 86 , 52 , 17 , 77 , 4 , 89 , 55 , 40 ] ,
220
+ [ 4 , 52 , 8 , 83 , 97 , 35 , 99 , 16 , 7 , 97 , 57 , 32 , 16 , 26 , 26 , 79 , 33 , 27 , 98 , 66 ] ,
153
221
[ 88 , 36 , 68 , 87 , 57 , 62 , 20 , 72 , 03 , 46 , 33 , 67 , 46 , 55 , 12 , 32 , 63 , 93 , 53 , 69 ] ,
154
- [ 04 , 42 , 16 , 73 , 38 , 25 , 39 , 11 , 24 , 94 , 72 , 18 , 08 , 46 , 29 , 32 , 40 , 62 , 76 , 36 ] ,
155
- [ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 04 , 36 , 16 ] ,
156
- [ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 01 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 05 , 54 ] ,
157
- [ 01 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 01 , 89 , 19 , 67 , 48 ] ,
222
+ [ 4 , 42 , 16 , 73 , 38 , 25 , 39 , 11 , 24 , 94 , 72 , 18 , 8 , 46 , 29 , 32 , 40 , 62 , 76 , 36 ] ,
223
+ [ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 , 16 ] ,
224
+ [ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 , 54 ] ,
225
+ [ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ] ,
158
226
] ;
159
227
228
+
160
229
var maxProduct = greatestProduct ( matrix ) ;
161
230
console . log ( maxProduct ) ;
0 commit comments