Skip to content

Commit 2efd2ff

Browse files
sofialevinsofialevin
authored andcommitted
Completed challenge 5 in array-methods.js.
1 parent e9664c4 commit 2efd2ff

1 file changed

Lines changed: 83 additions & 4 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,32 +57,111 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5757
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names into a new array called fullName.
5858
let fullName = [];
5959

60-
runners.forEach(function('first_name', 'last_name') {
61-
console.log('first_name', 'last_name')
60+
runners.forEach(function(runners) {
61+
fullName.push(runners.first_name + ' ' + runners.last_name)
6262
})
6363

6464
console.log(fullName);
6565

6666
// ==== Challenge 2: Use .map() ====
6767
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
6868
let allCaps = [];
69+
70+
allCaps = runners.map(function(runners) {
71+
return runners.first_name.toUpperCase();
72+
})
73+
6974
console.log(allCaps);
7075

7176
// ==== Challenge 3: Use .filter() ====
7277
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
78+
7379
let largeShirts = [];
80+
81+
largeShirts = runners.filter(function(runners) {
82+
return runners.shirt_size === 'L'
83+
})
84+
7485
console.log(largeShirts);
7586

7687
// ==== Challenge 4: Use .reduce() ====
7788
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
7889
let ticketPriceTotal = [];
90+
91+
ticketPriceTotal.push(runners.reduce(function(total, runners) {
92+
return total += runners.donation
93+
}, 0))
94+
7995
console.log(ticketPriceTotal);
8096

8197
// ==== Challenge 5: Be Creative ====
8298
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above.
8399

84-
// Problem 1
100+
// Problem 1 - Large donors
101+
102+
let largeDonors = [];
103+
104+
largeDonors = runners.filter(function(runner) {
105+
return runner.donation > 250
106+
})
107+
108+
let largeDonorsEmails = [];
109+
110+
largeDonorsEmails = largeDonors.map(function(donor) {
111+
return donor.email
112+
})
113+
114+
console.log(largeDonorsEmails);
85115

86116
// Problem 2
87117

88-
// Problem 3
118+
let xsShirts = 0;
119+
let sShirts = 0;
120+
let mShirts = 0;
121+
let lShirts = 0;
122+
let xlShirts = 0;
123+
let xxlShirts = 0;
124+
let xxxlShirts = 0;
125+
126+
let totalTshirts = [];
127+
128+
runners.forEach(function(runner) {
129+
if (runner.shirt_size === "XS") {
130+
xsShirts += 1;
131+
} else if (runner.shirt_size === "S") {
132+
sShirts += 1;
133+
} else if (runner.shirt_size === "M") {
134+
mShirts += 1;
135+
} else if (runner.shirt_size === "L") {
136+
lShirts += 1;
137+
} else if (runner.shirt_size === "XL") {
138+
xlShirts += 1;
139+
} else if (runner.shirt_size === "2XL") {
140+
xxlShirts += 1;
141+
} else if (runner.shirt_size === "3XL") {
142+
xxxlShirts += 1;
143+
}
144+
})
145+
146+
totalTshirts.push("XS: " + xsShirts, "S: " + sShirts, "M: " + mShirts, "L: " + lShirts, "XL: " + xlShirts, "2XL: " + xxlShirts, "3XL: " + xxxlShirts)
147+
console.log(totalTshirts)
148+
149+
// Problem 3
150+
151+
let companiesList = [];
152+
153+
companiesList = runners.map(function(item) {
154+
return item.company_name
155+
})
156+
157+
companiesList.sort();
158+
159+
let newArr = [companiesList[0]];
160+
161+
for (let i = 1; i < companiesList.length - 1; i++) {
162+
if (companiesList[i] !== companiesList[i - 1]) {
163+
newArr.push(companiesList[i]);
164+
}
165+
}
166+
167+
console.log(newArr)

0 commit comments

Comments
 (0)