Skip to content

Commit 4cffbaa

Browse files
committed
Iteration ironhack-labs#5 Done
1 parent ea69139 commit 4cffbaa

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/functions-and-arrays.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,34 @@ function sum() {}
4848
// Level 1: Array of numbers
4949
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
5050

51-
function averageNumbers() {}
51+
function averageNumbers(someArr) {
52+
if (someArr.length === 0) {
53+
return null;
54+
} else {
55+
let average = sumNumbers(someArr) / someArr.length;
56+
return average;
57+
}
58+
}
5259

60+
console.log(averageNumbers(numbersAvg));
5361

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

57-
function averageWordLength() { }
65+
function averageWordLength(moreArrays) {
66+
if (moreArrays.length === 0) {
67+
return null;
68+
} else {
69+
let arrLength = [];
70+
moreArrays.forEach(element => {
71+
arrLength.push(element.length);
72+
})
73+
let average = sumNumbers(arrLength) / arrLength.length;
74+
return average;
75+
}
76+
}
77+
78+
console.log(averageWordLength(wordsArr));
5879

5980
// Bonus - Iteration #4.1
6081
function avg() {}
@@ -74,9 +95,21 @@ const wordsUnique = [
7495
'bring'
7596
];
7697

77-
function uniquifyArray() {}
78-
98+
function uniquifyArray(wordsArr) {
99+
if (wordsArr.length === 0) {
100+
return null;
101+
} else {
102+
const uniqueElements = [ ];
103+
for (let i = 0; i < wordsArr.length; i += 1) {
104+
if (uniqueElements.includes(wordsArr[i]) === false) {
105+
uniqueElements.push(wordsArr[i]);
106+
}
107+
}
108+
return uniqueElements;
109+
}
110+
}
79111

112+
console.log(uniquifyArray(wordsUnique));
80113

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

0 commit comments

Comments
 (0)