Skip to content

Commit 145cdd4

Browse files
Gustavo JordaoGustavo Jordao
authored andcommitted
Finish remaining iterations from ironhack-labs#5-7
1 parent 35321c4 commit 145cdd4

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

starter-code/src/functions-and-arrays.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,26 @@ const wordsArr = [
6363
'correspond',
6464
'linen',
6565
'motif',
66-
'hole',
66+
'hole',
6767
'smell',
6868
'smart',
6969
'chaos',
7070
'fuel',
7171
'palace'
7272
];
7373

74+
function averageWordLength(arr) {
75+
let sumArr = 0;
76+
if (arr.length === 0) {
77+
return null;
78+
}
79+
for (let i = 0; i < arr.length; i++) {
80+
sumArr += arr[i].length;
81+
}
82+
let arrayAvg = sumArr / arr.length;
83+
return arrayAvg;
84+
};
85+
7486
// Iteration #5: Unique arrays
7587
const wordsUnique = [
7688
'crab',
@@ -86,6 +98,23 @@ const wordsUnique = [
8698
'bring'
8799
];
88100

101+
function uniquifyArray(arr) {
102+
if (arr.length === 0) {
103+
return [];
104+
}
105+
let uniqueArray = [];
106+
for (let i = 0; i < arr.length; i++) {
107+
if (uniqueArray.includes(arr[i]) == false) {
108+
uniqueArray.push(arr[i]);
109+
} else if (uniqueArray.includes(arr[i]) == true) {
110+
continue;
111+
}
112+
}
113+
return uniqueArray;
114+
}
115+
116+
uniquifyArray(wordsUnique);
117+
89118
// Iteration #6: Find elements
90119
const wordsFind = [
91120
'machine',
@@ -98,6 +127,18 @@ const wordsFind = [
98127
'disobedience'
99128
];
100129

130+
function doesWordExist(arr, word) {
131+
if (arr.length === 0) {
132+
return false;
133+
}
134+
for (let elem of arr) {
135+
if (elem === word) {
136+
return true;
137+
}
138+
}
139+
return false;
140+
}
141+
101142
// Iteration #7: Count repetition
102143
const wordsCount = [
103144
'machine',
@@ -113,6 +154,16 @@ const wordsCount = [
113154
'matter'
114155
];
115156

157+
function howManyTimes(arr, word) {
158+
let count = 0;
159+
for (let el of arr) {
160+
if (el === word) {
161+
count++
162+
}
163+
}
164+
return count;
165+
}
166+
116167
// Iteration #8: Bonus
117168

118169
const matrix = [

0 commit comments

Comments
 (0)