Skip to content

Commit 2f1fad4

Browse files
committed
Added solution for the challenges. Bonus ironhack-labs#8 is missing.
1 parent 9f4ff1e commit 2f1fad4

File tree

1 file changed

+92
-6
lines changed

1 file changed

+92
-6
lines changed

src/functions-and-arrays.js

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ function sumNumbers(arr) {
3131
return 0;
3232
}
3333

34-
sum(arr);
35-
return total;
36-
/*
34+
//sum(arr);
35+
//return total;
3736

3837
let total = 0;
3938

@@ -42,7 +41,6 @@ function sumNumbers(arr) {
4241
}
4342
return total;
4443
}
45-
*/
4644

4745
// Iteration #3 BONUS
4846

@@ -58,9 +56,11 @@ function sum(arr) {
5856
if(typeof arr[i] === "number") {
5957
total += arr[i];
6058
} else if (typeof arr[i] === "boolean") {
61-
total += total + arr[i];
59+
total += arr[i];
6260
} else if (typeof arr[i] === "string") {
6361
total += arr[i].length;
62+
} else if (typeof arr[i] === "object") {
63+
throw "Error: Unsupported data type sir or ma'am";
6464
}
6565
}
6666
return total;
@@ -76,14 +76,54 @@ function averageNumbers(arr) {
7676
return null;
7777
}
7878

79-
let average = sum(arr) / numbers.length;
79+
let average = sum(arr) / arr.length;
8080

8181
return average;
8282
}
8383

8484
// Level 2: Array of strings
8585
const wordsArr = ['seat', 'correspond', 'linen', 'motif', 'hole', 'smell', 'smart', 'chaos', 'fuel', 'palace'];
8686

87+
function averageWordLength(arr) {
88+
89+
if (arr.length === 0) {
90+
return null;
91+
}
92+
93+
let total = 0;
94+
95+
for(let i = 0; i < arr.length; i++) {
96+
total += arr[i].length;
97+
}
98+
let average = total / arr.length;
99+
100+
return average;
101+
}
102+
103+
function avg(arr) {
104+
105+
if (arr.length === 0) {
106+
return null;
107+
}
108+
109+
let total = 0;
110+
111+
for(let i = 0; i < arr.length; i++) {
112+
if(typeof arr[i] === "number") {
113+
total += arr[i];
114+
} else if (typeof arr[i] === "boolean") {
115+
total += arr[i];
116+
} else if (typeof arr[i] === "string") {
117+
total += arr[i].length;
118+
}
119+
}
120+
let average = (total / arr.length).toFixed(2);
121+
122+
// Return the value as a num and not a str
123+
return parseFloat(average);
124+
}
125+
126+
87127
// Iteration #5: Unique arrays
88128
const wordsUnique = [
89129
'crab',
@@ -99,9 +139,39 @@ const wordsUnique = [
99139
'bring'
100140
];
101141

142+
function uniquifyArray(arr) {
143+
144+
if (arr.length === 0) {
145+
return null;
146+
}
147+
148+
let newArr = [];
149+
150+
for(let i = 0; i < arr.length; i++) {
151+
if(newArr.indexOf(arr[i]) === -1) {
152+
newArr.push(arr[i]);
153+
}
154+
}
155+
return newArr;
156+
}
157+
102158
// Iteration #6: Find elements
103159
const wordsFind = ['machine', 'subset', 'trouble', 'starting', 'matter', 'eating', 'truth', 'disobedience'];
104160

161+
function doesWordExist(arr, word) {
162+
163+
if (arr.length === 0) {
164+
return null;
165+
}
166+
167+
for(let i = 0; i < arr.length; i++) {
168+
if(arr[i] === word) {
169+
return true;
170+
}
171+
}
172+
return false;
173+
}
174+
105175
// Iteration #7: Count repetition
106176
const wordsCount = [
107177
'machine',
@@ -117,6 +187,22 @@ const wordsCount = [
117187
'matter'
118188
];
119189

190+
function howManyTimes(arr, word) {
191+
192+
if (arr.length === 0) {
193+
return 0;
194+
}
195+
196+
let counter = 0;
197+
198+
for(let i = 0; i < arr.length; i++) {
199+
if (arr[i] === word) {
200+
counter += 1;
201+
}
202+
}
203+
return counter;
204+
}
205+
120206
// Iteration #8: Bonus
121207

122208
const matrix = [

0 commit comments

Comments
 (0)