Skip to content

Commit 7248d87

Browse files
committed
1 parent 72724d7 commit 7248d87

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

src/functions-and-arrays.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ function averageWordLength(arr){
7171
}
7272

7373
let sumWords = 0;
74-
for(let i = 0; i < arr.length; i++) {
75-
sumWords += arr[i].length;
74+
for(let i = 0; i < arr.length; i++) { // loop over array
75+
sumWords += arr[i].length; // add number of characters of word in array to sumWords
7676
}
77-
return sumWords / arr.length;
77+
return sumWords / arr.length; // total of sumWords divided by length of array
7878
}
7979

8080

@@ -93,9 +93,39 @@ const wordsUnique = [
9393
'bring'
9494
];
9595

96+
function uniquifyArray(arr){
97+
if(!arr.length) { // check if array is empty
98+
return null; // if so return null
99+
}
100+
101+
let newArr = [];
102+
for (let i = 0; i < arr.length; i++) {
103+
if (newArr.indexOf(arr[i]) === -1) {
104+
newArr.push(arr[i]);
105+
}
106+
}
107+
return newArr;
108+
}
109+
96110
// Iteration #6: Find elements
97111
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
98112

113+
function doesWordExist(arr, word) {
114+
if(!arr.length) { // check if array is empty
115+
return null; // if so return null
116+
}
117+
118+
for (var i = 0; i < arr.length; i++) { // loop over array
119+
if (arr[i] === word) { // check if specific word is in array
120+
return true; // if so, return true
121+
}
122+
}
123+
return false; // word is not in array, return false
124+
}
125+
126+
// doesWordExist(wordsFind, 'ajax'); //=> false
127+
// doesWordExist(wordsFind, 'trouble'); //=> true
128+
99129
// Iteration #7: Count repetition
100130
const wordsCount = [
101131
'machine',
@@ -111,6 +141,23 @@ const wordsCount = [
111141
'matter'
112142
];
113143

144+
function howManyTimes(arr, word) {
145+
if(!arr.length) { // check if array is empty
146+
return 0; // if so return null
147+
}
148+
149+
let countWord = 0;
150+
for (var i = 0; i < arr.length; i++) { // loop over array
151+
if(arr[i] === word){
152+
countWord++;
153+
}
154+
}
155+
return countWord;
156+
}
157+
158+
howManyTimes(wordsCount, 'matter'); //==> 4
159+
howManyTimes(wordsCount, 'ajax'); //==> 4
160+
114161
// Iteration #8: Bonus
115162

116163
const matrix = [

0 commit comments

Comments
 (0)