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 {
8
+ // return num1;
9
+ // }
10
+ return Math . max ( num1 , num2 ) ;
11
+ }
5
12
6
13
// Iteration #2: Find longest word
7
14
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
15
9
- function findLongestWord ( ) { }
16
+ function findLongestWord ( words ) {
17
+ if ( words . length === 0 ) return null ;
10
18
19
+ let longestWord = '' ;
11
20
21
+ for ( let i = 0 ; i < words . length ; i ++ ) {
22
+ if ( longestWord . length < words [ i ] . length ) {
23
+ longestWord = words [ i ] ;
24
+ }
25
+ }
26
+ return longestWord ;
27
+ }
12
28
13
29
// Iteration #3: Calculate the sum
14
30
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
31
16
- function sumNumbers ( ) { }
17
-
18
-
32
+ function sumNumbers ( numbers ) {
33
+ if ( numbers . length === 0 ) return 0 ;
34
+ let result = 0 ;
35
+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
36
+ result += numbers [ i ] ;
37
+ }
38
+ return result ;
39
+ }
19
40
20
41
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
22
-
23
-
42
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
43
+
44
+ function sum ( mixedArr ) {
45
+ if ( mixedArr . length === 0 ) return 0 ;
46
+
47
+ let result = 0 ;
48
+ for ( let i = 0 ; i < mixedArr . length ; i ++ ) {
49
+ const element = mixedArr [ i ] ;
50
+
51
+ if ( typeof element === 'string' ) {
52
+ result += element . length ;
53
+ } else if ( element === true ) {
54
+ result += 1 ;
55
+ } else if ( typeof element === 'object' ) {
56
+ throw new Error ( "Unsupported data type sir or ma'am" ) ;
57
+ } else {
58
+ result += mixedArr [ i ] ;
59
+ }
60
+ }
61
+ return result ;
62
+ }
24
63
25
64
// Iteration #4: Calculate the average
26
65
// Level 1: Array of numbers
27
66
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
67
29
- function averageNumbers ( ) { }
68
+ function averageNumbers ( numbers ) {
69
+ if ( numbers . length === 0 ) return null ;
70
+ if ( numbers . length === 1 ) return numbers [ 0 ] ;
30
71
72
+ let total = 0 ;
73
+ for ( let k = 0 ; k < numbers . length ; k ++ ) {
74
+ total += numbers [ k ] ;
75
+ }
76
+ return total / numbers . length ;
77
+ }
31
78
32
79
// Level 2: Array of strings
33
80
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
81
35
- function averageWordLength ( ) { }
82
+ function averageWordLength ( wordsArray ) {
83
+ if ( wordsArray . length === 0 ) return null ;
84
+ let total = 0 ;
85
+
86
+ for ( let j = 0 ; j < wordsArray . length ; j ++ ) {
87
+ total += wordsArray [ j ] . length ;
88
+ }
89
+
90
+ return total / wordsArray . length ;
91
+ }
36
92
37
93
// Bonus - Iteration #4.1
38
- function avg ( ) { }
94
+ const mixedDataArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
95
+
96
+ function avg ( mixedDataArr ) {
97
+ if ( mixedDataArr . length === 0 ) return null ;
98
+
99
+ let total = 0 ;
100
+
101
+ for ( let l = 0 ; l < mixedDataArr . length ; l ++ ) {
102
+ if ( typeof mixedDataArr [ l ] === 'string' ) {
103
+ total += mixedDataArr [ l ] . length ;
104
+ } else if ( typeof mixedDataArr [ l ] === 'boolean' ) {
105
+ if ( mixedDataArr === true ) total += 1 ;
106
+ } else {
107
+ total += mixedDataArr [ l ] ;
108
+ }
109
+ }
110
+ let avg = ( total / mixedDataArr . length ) . toFixed ( 2 ) ;
111
+
112
+ return Number ( avg ) ;
113
+ }
39
114
40
115
// Iteration #5: Unique arrays
41
116
const wordsUnique = [
@@ -52,16 +127,30 @@ const wordsUnique = [
52
127
'bring'
53
128
] ;
54
129
55
- function uniquifyArray ( ) { }
56
-
130
+ function uniquifyArray ( wordsUni ) {
131
+ if ( wordsUni . length === 0 ) return null ;
57
132
133
+ let cleanWordsUni = [ ] ;
134
+ wordsUni . forEach ( ( element ) => {
135
+ if ( ! cleanWordsUni . includes ( element ) ) {
136
+ cleanWordsUni . push ( element ) ;
137
+ }
138
+ } ) ;
139
+ return cleanWordsUni ;
140
+ }
58
141
59
142
// Iteration #6: Find elements
60
143
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
144
62
- function doesWordExist ( ) { }
63
-
145
+ function doesWordExist ( arraySearch , wordSearch ) {
146
+ if ( arraySearch . length === 0 ) return null ;
64
147
148
+ if ( arraySearch . includes ( wordSearch ) ) {
149
+ return true ;
150
+ } else {
151
+ return false ;
152
+ }
153
+ }
65
154
66
155
// Iteration #7: Count repetition
67
156
const wordsCount = [
@@ -78,9 +167,18 @@ const wordsCount = [
78
167
'matter'
79
168
] ;
80
169
81
- function howManyTimes ( ) { }
170
+ function howManyTimes ( arrayToCount , wordToCount ) {
171
+ let count = 0 ;
172
+ if ( arrayToCount . length === 0 ) return 0 ;
82
173
174
+ arrayToCount . forEach ( ( element ) => {
175
+ if ( element === wordToCount ) {
176
+ count ++ ;
177
+ }
178
+ } ) ;
83
179
180
+ return count ;
181
+ }
84
182
85
183
// Iteration #8: Bonus
86
184
const matrix = [
@@ -106,10 +204,10 @@ const matrix = [
106
204
[ 1 , 70 , 54 , 71 , 83 , 51 , 54 , 69 , 16 , 92 , 33 , 48 , 61 , 43 , 52 , 1 , 89 , 19 , 67 , 48 ]
107
205
] ;
108
206
109
- function greatestProduct ( ) { }
110
-
111
-
112
-
207
+ function greatestProduct ( matrix ) {
208
+ let biggest = 0 ;
209
+
210
+ }
113
211
114
212
// The following is required to make unit tests work.
115
213
/* Environment setup. Do not modify the below code. */
0 commit comments