1
1
// Iteration #1: Find the maximum
2
- function maxOfTwoNumbers ( ) { }
2
+ function maxOfTwoNumbers ( Num1 , Num2 ) {
3
+ if ( Num1 > Num2 ) {
4
+ return ( Num1 )
5
+ } else {
6
+ return ( Num2 )
7
+ }
8
+ }
9
+ console . log ( maxOfTwoNumbers ( 10 , 20 ) )
3
10
11
+ console . log ( Math . max ( 10 , 20 ) )
4
12
5
13
6
14
// Iteration #2: Find longest word
7
15
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
8
16
9
- function findLongestWord ( ) { }
17
+ function findLongestWord ( palabras ) {
18
+ let max = palabras . length ;
19
+ palabras . map ( palabra => max = Math . max ( max , palabra . length ) ) ;
20
+ result = palabras . filter ( palabra => palabra . length == max ) ;
21
+ return result ;
22
+ }
23
+
24
+ console . log ( findLongestWord ( words ) )
10
25
11
26
12
27
13
28
// Iteration #3: Calculate the sum
14
29
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
15
30
16
- function sumNumbers ( ) { }
17
-
18
-
31
+ function sumNumbers ( array ) {
32
+ const suma = array . reduce ( ( total , number ) => {
33
+ return total += number
34
+ } , 0 )
35
+ return suma
36
+ }
37
+ console . log ( sumNumbers ( numbers ) )
19
38
20
39
// Iteration #3.1 Bonus:
21
- function sum ( ) { }
40
+ const mixedArr = [ 6 , 12 , 'miami' , 1 , true , 'barca' , '200' , 'lisboa' , 8 , 10 ] ;
41
+ function sum ( ) {
42
+ const stringToNumber = mixedArr . map ( dato => {
43
+ if ( typeof dato === "string" ) {
44
+ return ( dato . length )
45
+ }
46
+ if ( typeof dato === "boolean" ) {
47
+ dato = 1
48
+ return ( dato )
49
+ }
50
+ return dato
51
+ } )
52
+ const Resultado = stringToNumber . reduce ( ( total , number ) => {
53
+ return total += number
54
+ } , 0 )
55
+ return Resultado
56
+ }
57
+
58
+ console . log ( sum ( mixedArr ) )
22
59
23
60
24
61
25
62
// Iteration #4: Calculate the average
26
63
// Level 1: Array of numbers
27
64
const numbersAvg = [ 2 , 6 , 9 , 10 , 7 , 4 , 1 , 9 ] ;
28
65
29
- function averageNumbers ( ) { }
66
+ function averageNumbers ( Numbers ) {
67
+ const Suma = Numbers . reduce ( ( total , number ) => {
68
+ return total += number
69
+ } )
70
+ return Suma / Numbers . length
71
+ }
72
+
73
+ console . log ( averageNumbers ( numbersAvg ) )
30
74
31
75
32
76
// Level 2: Array of strings
33
77
const wordsArr = [ 'seat' , 'correspond' , 'linen' , 'motif' , 'hole' , 'smell' , 'smart' , 'chaos' , 'fuel' , 'palace' ] ;
34
78
35
- function averageWordLength ( ) { }
36
-
79
+ function averageWordLength ( array ) {
80
+ const wordsArrLength = array . map ( palabra => {
81
+ return palabra . length
82
+ } )
83
+ const suma = wordsArrLength . reduce ( ( total , number ) => {
84
+ return total += number
85
+ } )
86
+ return suma / array . length
87
+ }
88
+ console . log ( averageWordLength ( wordsArr ) )
37
89
// Bonus - Iteration #4.1
38
- function avg ( ) { }
90
+
91
+ function avg ( array ) {
92
+ const stringToNumber = array . map ( dato => {
93
+ if ( typeof dato === "string" ) {
94
+ return ( dato . length )
95
+ }
96
+ if ( typeof dato === "boolean" ) {
97
+ dato = 1
98
+ return ( dato )
99
+ }
100
+ return dato
101
+ } )
102
+ const Resultado = stringToNumber . reduce ( ( total , number ) => {
103
+ return total += number
104
+ } , 0 )
105
+ return Resultado / array . length
106
+ }
107
+
108
+ console . log ( sum ( mixedArr ) )
39
109
40
110
// Iteration #5: Unique arrays
41
111
const wordsUnique = [
@@ -52,15 +122,31 @@ const wordsUnique = [
52
122
'bring'
53
123
] ;
54
124
55
- function uniquifyArray ( ) { }
125
+ function uniquifyArray ( palabras ) {
126
+
127
+ let result = palabras . filter ( ( palabra , index ) => {
128
+ return palabras . indexOf ( palabra ) === index ;
129
+ } )
130
+ return result ;
131
+ }
56
132
133
+ console . log ( uniquifyArray ( wordsUnique ) )
57
134
58
135
59
136
// Iteration #6: Find elements
60
137
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
138
+ const palabraAbuscar = "trouble"
139
+
140
+ function doesWordExist ( array ) {
141
+ const find = array . find ( palabra => palabraAbuscar === palabra )
142
+ if ( find ) {
143
+ return "Si existe"
144
+ } else {
145
+ return "No existe"
146
+ }
147
+ }
61
148
62
- function doesWordExist ( ) { }
63
-
149
+ console . log ( doesWordExist ( wordsFind ) )
64
150
65
151
66
152
// Iteration #7: Count repetition
@@ -77,9 +163,15 @@ const wordsCount = [
77
163
'disobedience' ,
78
164
'matter'
79
165
] ;
166
+ const palabra = "matter"
167
+ function howManyTimes ( array ) {
168
+ const palabraRepetida = array . filter ( palabraDeArray => {
169
+ return palabraDeArray . includes ( palabra )
170
+ } )
171
+ return palabraRepetida
80
172
81
- function howManyTimes ( ) { }
82
-
173
+ }
174
+ console . log ( howManyTimes ( wordsCount ) . length )
83
175
84
176
85
177
// Iteration #8: Bonus
@@ -108,7 +200,7 @@ const matrix = [
108
200
109
201
function greatestProduct ( ) { }
110
202
111
-
203
+ //No entendí Iteration 8
112
204
113
205
114
206
// The following is required to make unit tests work.
0 commit comments