1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
2
+ function maxOfTwoNumbers ( numberOne , numberTwo ) {
3
+ if ( numberOne > numberTwo ) {
4
+ return numberOne ;
5
+ }
6
+ return numberTwo ;
7
+ }
3
8
4
9
5
10
6
11
// Iteration #2: Find longest word
7
12
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
-
9
- function findLongestWord ( ) { }
10
-
13
+ let testLng = 0 ;
14
+ let longest ;
15
+ function findLongestWord ( arrLongest ) {
16
+ for ( let i = 0 ; i <= arrLongest . length ; i ++ ) {
17
+ if ( arrLongest . length === 0 ) {
18
+ return null ;
19
+ }
20
+ if ( arrLongest . length === 1 ) {
21
+ return arrLongest [ i ] ;
22
+ }
23
+ if ( arrLongest . length > 1 ) {
24
+ return arrLongest . reduce ( ( longestItem , currentItem ) => {
25
+ if ( longestItem . length === currentItem ) {
26
+ return longestItem ;
27
+ }
28
+ if ( longestItem . length < currentItem . length ) {
29
+ return currentItem ;
30
+ }
31
+ return longestItem ;
32
+ } , "" ) ;
33
+ }
34
+ }
35
+
36
+ }
37
+
11
38
12
39
13
40
// Iteration #3: Calculate the sum
14
41
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
42
16
- function sumNumbers ( ) { }
17
-
43
+ function sumNumbers ( arr ) {
44
+ let sum = 0 ;
45
+ arr . forEach ( ( item ) => {
46
+ sum = sum + item ;
47
+ } ) ;
48
+ return sum ;
49
+ }
18
50
19
51
20
52
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
53
+ function sum ( arr ) {
54
+ let finalSum = 0 ;
55
+ arr . forEach ( ( item ) => {
56
+ finalSum = finalSum + item . length ;
57
+ } ) ;
58
+ return finalSum ;
59
+ }
60
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
22
61
23
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 ( arr ) {
69
+ if ( arr . length > 0 ) {
70
+ let avg = sumNumbers ( arr ) / arr . length ;
71
+ return avg ;
72
+ }
73
+ return null ;
74
+ }
30
75
31
76
32
77
// Level 2: Array of strings
33
78
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
79
35
- function averageWordLength ( ) { }
80
+ function averageWordLength ( arr ) {
81
+ let arrSum = '' ;
82
+ arr . forEach ( ( string ) => {
83
+ arrSum = arrSum + string ;
84
+ } ) ;
85
+ let averageOfStrings = arrSum . length / arr . length ;
86
+ if ( averageOfStrings > 0 ) {
87
+ return averageOfStrings ;
88
+ }
89
+ return null ;
90
+ }
36
91
37
92
// Bonus - Iteration #4.1
38
93
function avg ( ) { }
@@ -52,14 +107,34 @@ const wordsUnique = [
52
107
'bring'
53
108
] ;
54
109
55
- function uniquifyArray ( ) { }
110
+ function uniquifyArray ( arr ) {
111
+ if ( arr . length === 0 ) {
112
+ return null ;
113
+ }
114
+ return Array . from ( new Set ( arr ) ) ;
115
+ }
116
+
56
117
57
118
58
119
59
120
// Iteration #6: Find elements
60
121
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
61
122
62
- function doesWordExist ( ) { }
123
+ function doesWordExist ( arrWords ) {
124
+ for ( let i = 0 ; i <= arrWords ; i ++ ) {
125
+ if ( arrWords . length === 0 ) {
126
+ return null ;
127
+ }
128
+ if ( arrWords >= 1 ) {
129
+ return arr . reduce ( ( magicWord , word ) => {
130
+ if ( magicWord === word ) {
131
+ return true ;
132
+ }
133
+ } , "" ) ;
134
+ }
135
+ } return false ;
136
+ }
137
+
63
138
64
139
65
140
@@ -78,7 +153,17 @@ const wordsCount = [
78
153
'matter'
79
154
] ;
80
155
81
- function howManyTimes ( ) { }
156
+ function howManyTimes ( count ) {
157
+ for ( let i = 0 ; i <= count . length ; i ++ ) {
158
+ let sumCount = 0 ;
159
+ if ( count . length === 0 ) {
160
+ return 0 ;
161
+ }
162
+ if ( count [ i ] === i + 1 ) {
163
+ return
164
+ }
165
+ }
166
+ }
82
167
83
168
84
169
0 commit comments