@@ -134,7 +134,7 @@ const wordsUnique = [
134
134
// let filtered = [];
135
135
// for (let i=0; i < wordArr.length; i++) {
136
136
// if (wordArr.indexOf(wordArr[i], i+1) === -1) {
137
- // filtered.push (wordArr[i]);
137
+ // filtered.unshift (wordArr[i]);
138
138
// }
139
139
// }
140
140
// return filtered;
@@ -155,9 +155,18 @@ function uniquifyArray(wordArr) {
155
155
// console.log(uniquifyArray(['Cat', 'Dog', 'Cow'])); // ['Cat', 'Dog', 'Cow']
156
156
// console.log(uniquifyArray(['iPhone', 'Samsung', 'Android', 'iOS', 'iPhone', 'Samsung', 'Nokia', 'Blackberry', 'Android'])); //['iPhone', 'Samsung', 'Android', 'iOS', 'Nokia', 'Blackberry']
157
157
158
+
159
+
158
160
// Bonus - Iteration #5.1: Unique arrays using Set
159
161
160
- function uniquifyArrayWithSet ( ) { }
162
+ function uniquifyArrayWithSet ( wordArr ) {
163
+ // let set = new Set(wordArr);
164
+ // return [...set];
165
+ return [ ...( new Set ( wordArr ) ) ] ;
166
+ }
167
+
168
+ console . log ( uniquifyArrayWithSet ( wordsUnique ) ) ; //['crab','poison','contagious','simple','bring','sharp','playground','communion']
169
+
161
170
162
171
// Iteration #6: Find elements
163
172
const wordsFind = [ 'machine' , 'subset' , 'trouble' , 'starting' , 'matter' , 'eating' , 'truth' , 'disobedience' ] ;
@@ -173,6 +182,7 @@ function doesWordExist(wordArr, word) {
173
182
// console.log(doesWordExist(['machine', 'poison', 'eat', 'apple', 'horse'], 'ratatouille')); // false
174
183
// console.log(doesWordExist(['pizza', 'sandwich', 'snack', 'soda', 'book', 'computer'], 'book')); // true
175
184
185
+
176
186
// Iteration #7: Count repetition
177
187
const wordsCount = [
178
188
'machine' ,
@@ -188,7 +198,26 @@ const wordsCount = [
188
198
'matter'
189
199
] ;
190
200
191
- function howManyTimes ( ) { }
201
+ function howManyTimes ( wordArr , word ) {
202
+ if ( wordArr . length === 0 ) {
203
+ return 0 ;
204
+ }
205
+ let counter = 0 ;
206
+ wordArr . forEach ( item => {
207
+ if ( item === word ) {
208
+ counter ++ ;
209
+ }
210
+ } ) ;
211
+ return counter ;
212
+ }
213
+
214
+ // console.log(howManyTimes(wordsCount, "matter")); // 4
215
+ // console.log(howManyTimes(['basketball', 'football', 'tennis'], 'tennis')); // 1
216
+
217
+
218
+
219
+
220
+
192
221
193
222
// The following is required to make unit tests work.
194
223
/* Environment setup. Do not modify the below code. */
0 commit comments