Skip to content

Commit cd43119

Browse files
committed
add iteration 5 and 6
1 parent 6fd8f80 commit cd43119

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/functions-and-arrays.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,42 @@ const wordsUnique = [
111111
'bring'
112112
];
113113

114-
function uniquifyArray() {}
114+
function uniquifyArray(arr) {
115+
const isEmptyArr = arr.length === 0;
116+
if (isEmptyArr) {
117+
return null;
118+
} else {
119+
let uniqueArr = [];
120+
for (word of arr) {
121+
const alreadyExists = uniqueArr.indexOf(word) >= 0;
122+
if (alreadyExists) {
123+
continue;
124+
}
125+
uniqueArr.push(word);
126+
}
127+
return uniqueArr;
128+
}
129+
}
115130

116131
// Iteration #6: Find elements
117132
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
118133

119-
function doesWordExist() {}
134+
function doesWordExist(arr, wordToFind) {
135+
const isEmptyArr = arr.length === 0;
136+
if (isEmptyArr) {
137+
return null;
138+
} else {
139+
let wordExists;
140+
for (word of arr) {
141+
wordExists = word === wordToFind;
142+
if (wordExists) {
143+
return wordExists;
144+
exit;
145+
}
146+
}
147+
return wordExists;
148+
}
149+
}
120150

121151
// Iteration #7: Count repetition
122152
const wordsCount = [

0 commit comments

Comments
 (0)