Skip to content

Commit c7d1bc3

Browse files
committed
Completed Task 3: Array methods
1 parent e85d94d commit c7d1bc3

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ This task focuses on getting practice with callback functions by giving you an a
1818

1919
### Task 3: Array Methods
2020
Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array with 50 objects in it. The [array-methods.js](assignments/array-methods.js) file contains several challenges built around a fundraising 5K fun run event.
21-
* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
22-
* [ ] Complete the problems provided to you
23-
* [ ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
21+
* [x] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
22+
* [x] Complete the problems provided to you
23+
* [x] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
2424

2525
### Task 4: Closures
2626
We have learned that closures allow us to access values in scope that have already been invoked.

assignments/array-methods.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,81 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5656
// ==== Challenge 1: Use .forEach() ====
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 = [];
59+
runners.forEach((runner) => {
60+
fullName.push(`${runner.first_name} ${runner.last_name}`);
61+
});
5962
console.log(fullName);
6063

6164
// ==== Challenge 2: Use .map() ====
6265
// 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
63-
let allCaps = [];
66+
let allCaps = runners.map((runner) => {
67+
return `${runner.first_name.toUpperCase()}`;
68+
});
6469
console.log(allCaps);
6570

6671
// ==== Challenge 3: Use .filter() ====
6772
// 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
68-
let largeShirts = [];
73+
let largeShirts = runners.filter((runner) => {
74+
return runner.shirt_size === 'L';
75+
});
6976
console.log(largeShirts);
7077

7178
// ==== Challenge 4: Use .reduce() ====
7279
// 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
73-
let ticketPriceTotal = [];
80+
let ticketPriceTotal = runners.reduce((total, runner) => {
81+
return total + runner.donation;
82+
}, 0);
7483
console.log(ticketPriceTotal);
7584

7685
// ==== Challenge 5: Be Creative ====
7786
// 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 solve 3 unique problems using one or many of the array methods listed above.
7887

79-
// Problem 1
88+
// Problem 1 - An e-mail list for sending out news and updates to all the runners
89+
let emails = [];
90+
runners.forEach((runner) => {
91+
emails.push(runner.email);
92+
});
93+
console.log(emails);
8094

81-
// Problem 2
95+
// Problem 2 - The total donations for a given company (using Skinix as example)
96+
let companyTotalDonations = (runners, company_name) => {
97+
// get only the runners for that company
98+
let runnersForCompany = runners.filter((runner) => {
99+
return runner.company_name === company_name;
100+
});
101+
// return the total of that company's donations
102+
return runnersForCompany.reduce((total, runner) => {
103+
return total + runner.donation;
104+
}, 0);
105+
};
106+
console.log(companyTotalDonations(runners, 'Skinix'));
82107

83-
// Problem 3
108+
// Problem 3 - An array that holds objects of each company name and their total donation
109+
let totalDonationsByCompany = (runners) => {
110+
// get the full list of company names
111+
let companies = [];
112+
runners.forEach((runner) => {
113+
companies.push(runner.company_name);
114+
});
115+
116+
// remove duplicates from companies array
117+
companies.sort();
118+
companiesNoDuplicates = [];
119+
for (let i = 0; i < companies.length; i++) {
120+
if (companies[i] != companies[i+1]) {
121+
companiesNoDuplicates.push(companies[i]);
122+
}
123+
}
124+
125+
// for each company name use function from problem 2 (companyTotalDonations) to get total donations
126+
companiesAndTotals = [];
127+
companiesNoDuplicates.forEach((company_name) => {
128+
companiesAndTotals.push({
129+
"company_name": company_name,
130+
"total_donation": companyTotalDonations(runners, company_name)
131+
});
132+
});
133+
134+
return companiesAndTotals; // return the array with companies and their total donations
135+
};
136+
console.log(totalDonationsByCompany(runners));

0 commit comments

Comments
 (0)