Skip to content

Commit ce89fd7

Browse files
committed
Bonus - Iteration ironhack-labs#5.1: The Set Object and Iteration ironhack-labs#7: Count repetition
1 parent 590f631 commit ce89fd7

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/functions-and-arrays.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const wordsUnique = [
134134
// let filtered = [];
135135
// for (let i=0; i < wordArr.length; i++) {
136136
// if (wordArr.indexOf(wordArr[i], i+1) === -1) {
137-
// filtered.push(wordArr[i]);
137+
// filtered.unshift(wordArr[i]);
138138
// }
139139
// }
140140
// return filtered;
@@ -155,9 +155,18 @@ function uniquifyArray(wordArr) {
155155
// console.log(uniquifyArray(['Cat', 'Dog', 'Cow'])); // ['Cat', 'Dog', 'Cow']
156156
// console.log(uniquifyArray(['iPhone', 'Samsung', 'Android', 'iOS', 'iPhone', 'Samsung', 'Nokia', 'Blackberry', 'Android'])); //['iPhone', 'Samsung', 'Android', 'iOS', 'Nokia', 'Blackberry']
157157

158+
159+
158160
// Bonus - Iteration #5.1: Unique arrays using Set
159161

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+
161170

162171
// Iteration #6: Find elements
163172
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
@@ -173,6 +182,7 @@ function doesWordExist(wordArr, word) {
173182
// console.log(doesWordExist(['machine', 'poison', 'eat', 'apple', 'horse'], 'ratatouille')); // false
174183
// console.log(doesWordExist(['pizza', 'sandwich', 'snack', 'soda', 'book', 'computer'], 'book')); // true
175184

185+
176186
// Iteration #7: Count repetition
177187
const wordsCount = [
178188
'machine',
@@ -188,7 +198,26 @@ const wordsCount = [
188198
'matter'
189199
];
190200

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+
192221

193222
// The following is required to make unit tests work.
194223
/* Environment setup. Do not modify the below code. */

0 commit comments

Comments
 (0)