Skip to content

Commit 9fe71bd

Browse files
all done except bonus ironhack-labs#8
1 parent 572057c commit 9fe71bd

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

src/functions-and-arrays.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,33 @@ function sum(list) {
5656
// Level 1: Array of numbers
5757
const numbersAvg = [2, 6, 9, 10, 7, 4, 1, 9];
5858

59+
//we reuse function sum because it's already made to cover all bases
5960
function averageNumbers(numbers) {
61+
if (numbers.length === 0) {
62+
return null;
63+
}
6064
return sum(numbers)/numbers.length;
6165
}
6266

6367

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

67-
function averageWordLength() { }
71+
//function 'sum' already accepts string data types
72+
function averageWordLength(wordsArr) {
73+
if (wordsArr.length === 0) {
74+
return null;
75+
}
76+
return sum(wordsArr)/wordsArr.length;
77+
}
6878

6979
// Bonus - Iteration #4.1
70-
function avg() {}
80+
function avg(list) {
81+
if (list.length === 0) {
82+
return null;
83+
}
84+
return sum(list)/list.length;
85+
}
7186

7287
// Iteration #5: Unique arrays
7388
const wordsUnique = [
@@ -84,14 +99,29 @@ const wordsUnique = [
8499
'bring'
85100
];
86101

87-
function uniquifyArray() {}
102+
function uniquifyArray(list) {
103+
//check if the list is empty
104+
if (list.length === 0) {
105+
return null;
106+
}
107+
//We will use a set. A set is basically an array but it can only hold unique values. Let's make a set out of the array.
108+
let result = new Set(list);
109+
//The 'set' is actually an object.
110+
//We need to give the values stored in the set into an array back again. Just put it into brackets to 'array-fy' the set, then use the spread operator to separate the values into different elements of the array.
111+
return [...result];
112+
}
88113

89114

90115

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

94-
function doesWordExist() {}
119+
function doesWordExist(list, word) {
120+
if (list.length === 0) {
121+
return null;
122+
}
123+
return list.includes(word);
124+
}
95125

96126

97127

@@ -110,7 +140,15 @@ const wordsCount = [
110140
'matter'
111141
];
112142

113-
function howManyTimes() {}
143+
function howManyTimes(list, word) {
144+
let result = 0;
145+
for (let elem of list) {
146+
if (elem === word) {
147+
result++;
148+
}
149+
}
150+
return result;
151+
}
114152

115153

116154

@@ -138,7 +176,9 @@ const matrix = [
138176
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
139177
];
140178

141-
function greatestProduct() {}
179+
function greatestProduct(matrix) {
180+
181+
}
142182

143183

144184

0 commit comments

Comments
 (0)