1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
3
-
4
-
2
+ function maxOfTwoNumbers ( num1 , num2 ) {
3
+ if ( num1 > num2 ) {
4
+ return num1 ;
5
+ } else if ( num1 < num2 ) {
6
+ return num2 ;
7
+ } else return num1 ;
8
+ }
5
9
6
10
// Iteration #2: Find longest word
7
- const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
-
9
- function findLongestWord ( ) { }
11
+ const words = [
12
+ "mystery" ,
13
+ "brother" ,
14
+ "aviator" ,
15
+ "crocodile" ,
16
+ "pearl" ,
17
+ "orchard" ,
18
+ "crackpot" ,
19
+ ] ;
10
20
21
+ function findLongestWord ( arr ) {
22
+ if ( arr . length === 0 ) {
23
+ return null ;
24
+ } else {
25
+ let words = " " ;
26
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
27
+ if ( words . length < arr [ i ] . length ) {
28
+ words = arr [ i ] ;
29
+ }
30
+ }
31
+ return words ;
32
+ }
33
+ }
11
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 ( ) { }
39
+ function sumNumbers ( arr ) {
40
+ let sum = 0 ;
41
+
42
+ if ( arr . length === 0 ) {
43
+ return 0 ;
44
+ } else {
45
+ arr . forEach ( ( num ) => {
46
+ sum += num ;
47
+ } ) ;
48
+ return sum ;
49
+ }
50
+ }
17
51
18
52
53
+ ///////////////////////////////////////////////////////////////////////
19
54
20
55
// Iteration #3.1 Bonus:
21
56
function sum ( ) { }
22
-
57
+ ///////////////////////////////////////////////////////////////////////
23
58
24
59
25
60
// Iteration #4: Calculate the average
26
61
// Level 1: Array of numbers
27
62
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
63
29
- function averageNumbers ( ) { }
64
+ function averageNumbers ( arr ) {
65
+ let sum = 0 ;
66
+ if ( arr . length === 0 ) {
67
+ return null ; }
68
+ else {
69
+ for ( i = 0 ; i < arr . length ; i ++ ) {
70
+ sum += arr [ i ] ;
71
+ }
72
+ return sum / arr . length ;
73
+ }
74
+ }
75
+
76
+
30
77
31
78
32
79
// Level 2: Array of strings
33
- const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
80
+ const wordsArr = [
81
+ "seat" ,
82
+ "correspond" ,
83
+ "linen" ,
84
+ "motif" ,
85
+ "hole" ,
86
+ "smell" ,
87
+ "smart" ,
88
+ "chaos" ,
89
+ "fuel" ,
90
+ "palace" ,
91
+ ] ;
34
92
35
- function averageWordLength ( ) { }
93
+ function averageWordLength ( ) { }
36
94
37
95
// Bonus - Iteration #4.1
38
96
function avg ( ) { }
39
97
40
98
// Iteration #5: Unique arrays
41
99
const wordsUnique = [
42
- ' crab' ,
43
- ' poison' ,
44
- ' contagious' ,
45
- ' simple' ,
46
- ' bring' ,
47
- ' sharp' ,
48
- ' playground' ,
49
- ' poison' ,
50
- ' communion' ,
51
- ' simple' ,
52
- ' bring'
100
+ " crab" ,
101
+ " poison" ,
102
+ " contagious" ,
103
+ " simple" ,
104
+ " bring" ,
105
+ " sharp" ,
106
+ " playground" ,
107
+ " poison" ,
108
+ " communion" ,
109
+ " simple" ,
110
+ " bring" ,
53
111
] ;
54
112
55
113
function uniquifyArray ( ) { }
56
114
57
-
58
-
59
115
// Iteration #6: Find elements
60
- const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
116
+ const wordsFind = [
117
+ "machine" ,
118
+ "subset" ,
119
+ "trouble" ,
120
+ "starting" ,
121
+ "matter" ,
122
+ "eating" ,
123
+ "truth" ,
124
+ "disobedience" ,
125
+ ] ;
61
126
62
127
function doesWordExist ( ) { }
63
128
64
-
65
-
66
129
// Iteration #7: Count repetition
67
130
const wordsCount = [
68
- ' machine' ,
69
- ' matter' ,
70
- ' subset' ,
71
- ' trouble' ,
72
- ' starting' ,
73
- ' matter' ,
74
- ' eating' ,
75
- ' matter' ,
76
- ' truth' ,
77
- ' disobedience' ,
78
- ' matter'
131
+ " machine" ,
132
+ " matter" ,
133
+ " subset" ,
134
+ " trouble" ,
135
+ " starting" ,
136
+ " matter" ,
137
+ " eating" ,
138
+ " matter" ,
139
+ " truth" ,
140
+ " disobedience" ,
141
+ " matter" ,
79
142
] ;
80
143
81
144
function howManyTimes ( ) { }
82
145
83
-
84
-
85
146
// Iteration #8: Bonus
86
147
const matrix = [
87
148
[ 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 ] ,
149
+ [
150
+ 49 , 49 , 99 , 40 , 17 , 81 , 18 , 57 , 60 , 87 , 17 , 40 , 98 , 43 , 69 , 48 , 4 , 56 , 62 ,
151
+ 0 ,
152
+ ] ,
153
+ [
154
+ 81 , 49 , 31 , 73 , 55 , 79 , 14 , 29 , 93 , 71 , 40 , 67 , 53 , 88 , 30 , 3 , 49 , 13 , 36 ,
155
+ 65 ,
156
+ ] ,
90
157
[ 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 ] ,
158
+ [
159
+ 22 , 31 , 16 , 71 , 51 , 67 , 63 , 89 , 41 , 92 , 36 , 54 , 22 , 40 , 40 , 28 , 66 , 33 , 13 ,
160
+ 80 ,
161
+ ] ,
162
+ [
163
+ 24 , 47 , 32 , 60 , 99 , 3 , 45 , 2 , 44 , 75 , 33 , 53 , 78 , 36 , 84 , 20 , 35 , 17 , 12 ,
164
+ 50 ,
165
+ ] ,
166
+ [
167
+ 32 , 98 , 81 , 28 , 64 , 23 , 67 , 10 , 26 , 38 , 40 , 67 , 59 , 54 , 70 , 66 , 18 , 38 , 64 ,
168
+ 70 ,
169
+ ] ,
170
+ [
171
+ 67 , 26 , 20 , 68 , 2 , 62 , 12 , 20 , 95 , 63 , 94 , 39 , 63 , 8 , 40 , 91 , 66 , 49 , 94 ,
172
+ 21 ,
173
+ ] ,
174
+ [
175
+ 24 , 55 , 58 , 5 , 66 , 73 , 99 , 26 , 97 , 17 , 78 , 78 , 96 , 83 , 14 , 88 , 34 , 89 , 63 ,
176
+ 72 ,
177
+ ] ,
96
178
[ 21 , 36 , 23 , 9 , 75 , 0 , 76 , 44 , 20 , 45 , 35 , 14 , 0 , 61 , 33 , 97 , 34 , 31 , 33 , 95 ] ,
97
179
[ 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 ] ,
180
+ [
181
+ 16 , 39 , 5 , 42 , 96 , 35 , 31 , 47 , 55 , 58 , 88 , 24 , 0 , 17 , 54 , 24 , 36 , 29 , 85 ,
182
+ 57 ,
183
+ ] ,
99
184
[ 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 ] ,
185
+ [
186
+ 19 , 80 , 81 , 68 , 5 , 94 , 47 , 69 , 28 , 73 , 92 , 13 , 86 , 52 , 17 , 77 , 4 , 89 , 55 ,
187
+ 40 ,
188
+ ] ,
101
189
[ 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 ]
190
+ [
191
+ 88 , 36 , 68 , 87 , 57 , 62 , 20 , 72 , 3 , 46 , 33 , 67 , 46 , 55 , 12 , 32 , 63 , 93 , 53 ,
192
+ 69 ,
193
+ ] ,
194
+ [
195
+ 4 , 42 , 16 , 73 , 38 , 25 , 39 , 11 , 24 , 94 , 72 , 18 , 8 , 46 , 29 , 32 , 40 , 62 , 76 ,
196
+ 36 ,
197
+ ] ,
198
+ [
199
+ 20 , 69 , 36 , 41 , 72 , 30 , 23 , 88 , 34 , 62 , 99 , 69 , 82 , 67 , 59 , 85 , 74 , 4 , 36 ,
200
+ 16 ,
201
+ ] ,
202
+ [
203
+ 20 , 73 , 35 , 29 , 78 , 31 , 90 , 1 , 74 , 31 , 49 , 71 , 48 , 86 , 81 , 16 , 23 , 57 , 5 ,
204
+ 54 ,
205
+ ] ,
206
+ [
207
+ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 ,
208
+ 48 ,
209
+ ] ,
107
210
] ;
108
211
109
212
function greatestProduct ( ) { }
110
213
111
-
112
-
113
-
114
214
// The following is required to make unit tests work.
115
215
/* Environment setup. Do not modify the below code. */
116
- if ( typeof module !== ' undefined' ) {
216
+ if ( typeof module !== " undefined" ) {
117
217
module . exports = {
118
218
maxOfTwoNumbers,
119
219
findLongestWord,
@@ -125,6 +225,6 @@ if (typeof module !== 'undefined') {
125
225
uniquifyArray,
126
226
doesWordExist,
127
227
howManyTimes,
128
- greatestProduct
228
+ greatestProduct,
129
229
} ;
130
230
}
0 commit comments