@@ -17,13 +17,33 @@ console.log("the max of two numbers is " + maxOfTwoNumbers(2,4));
17
17
// Iteration #2: Find longest word
18
18
const words = [ 'mystery' , 'brother' , 'aviator' , 'crocodile' , 'pearl' , 'orchard' , 'crackpot' ] ;
19
19
20
- findLongestWord = ( words ) => words . reduce ( ( a , b ) =>
21
- { if ( b . length > a . length ) return b ; else return a } ) ;
20
+
21
+ function findLongestWord ( words ) {
22
+
23
+ let longestWord = "" ;
24
+
25
+ for ( var i = 0 ; i < words . length ; i ++ ) {
26
+
27
+ if ( longestWord . length < words [ i ] . length ) {
28
+
29
+ longestWord = words [ i ] ;
30
+
31
+ } else if ( longestWord . length === words [ i ] . length ) {
32
+
33
+ var morelongestWords = [ ] ;
34
+ morelongestWords . push ( words [ i ] , longestWord ) ;
35
+
36
+ }
37
+ }
38
+ return longestWord ;
39
+ }
40
+
22
41
23
42
console . log ( "the longest word is " + findLongestWord ( words ) ) ;
24
43
25
44
26
45
46
+
27
47
// Iteration #3: Calculate the sum
28
48
const numbers = [ 6 , 12 , 1 , 18 , 13 , 16 , 2 , 1 , 8 , 10 ] ;
29
49
@@ -113,14 +133,38 @@ const wordsUnique = [
113
133
'bring'
114
134
] ;
115
135
116
- function uniquifyArray ( ) { }
136
+ function uniquifyArray ( arr ) {
137
+ let newArr = [ ]
138
+ if ( ! arr . length ) {
139
+ return null
140
+ }
141
+ if ( arr . length ) {
142
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
143
+ if ( ! newArr . includes ( wordsUnique [ i ] ) ) {
144
+ newArr . push ( arr [ i ] ) ;
145
+ } }
146
+ }
147
+ return newArr ;
148
+ }
149
+ console . log ( "unique array is " + uniquifyArray ( wordsUnique ) )
117
150
118
151
119
152
120
153
// Iteration #6: Find elements
121
154
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
122
155
123
- function doesWordExist ( ) { }
156
+ function doesWordExist ( wordsFind , wordToSearch ) {
157
+ if ( wordsFind . length === 0 ) {
158
+ return false ;
159
+ }
160
+ var longitud = wordsFind . length ;
161
+ for ( var i = 0 ; i < longitud ; i ++ ) {
162
+ if ( wordsFind [ i ] === wordToSearch ) {
163
+ return true ;
164
+ }
165
+ }
166
+ return false ;
167
+ }
124
168
125
169
126
170
@@ -139,7 +183,19 @@ const wordsCount = [
139
183
'matter'
140
184
] ;
141
185
142
- function howManyTimes ( ) { }
186
+
187
+ function howManyTimes ( arr , wordtoSearch ) {
188
+ if ( ! arr . length ) {
189
+ return 0 ;
190
+ }
191
+
192
+ let count = 0
193
+ for ( let key of arr ) {
194
+ if ( key === wordtoSearch )
195
+ count ++ ;
196
+ }
197
+ return count ;
198
+ }
143
199
144
200
145
201
0 commit comments