Skip to content

Commit 392effd

Browse files
Array Methods MVP
1 parent ed0323e commit 392effd

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,39 @@ 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-
console.log(fullName);
59+
runners.forEach(runner => fullName.push(`${runner.first_name} ${runner.last_name}`));
60+
console.log('Arrays Challenge 1 ', fullName);
6061

6162
// ==== Challenge 2: Use .map() ====
6263
// 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 = [];
64-
console.log(allCaps);
64+
let allCaps = runners.map(runner => runner.first_name.toUpperCase());
65+
console.log('Arrays Challenge 2 ', allCaps);
6566

6667
// ==== Challenge 3: Use .filter() ====
6768
// 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 = [];
69-
console.log(largeShirts);
69+
let largeShirts = runners.filter(runner => runner.shirt_size === "L");
70+
console.log('Arrays Challenge 3 ', largeShirts);
7071

7172
// ==== Challenge 4: Use .reduce() ====
7273
// 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 = [];
74-
console.log(ticketPriceTotal);
74+
let ticketPriceTotal = runners.reduce((accumulator, runner) => {
75+
return accumulator + runner.donation; //Shoutout to programming with mosh
76+
}, 0)
77+
78+
console.log('Arrays Challenge 4 ',ticketPriceTotal);
7579

7680
// ==== Challenge 5: Be Creative ====
7781
// 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.
7882

7983
// Problem 1
84+
const highDonations = runners.filter(runner => runner.donation >= 200)
85+
.map(runner => `${runner.first_name} ${runner.last_name} has donated ${runner.donation}`);
86+
console.log('Array Challenge 5 ', highDonations);
8087

8188
// Problem 2
89+
const sortedRunners = runners.map(runner => runner.company_name).sort();
90+
console.log('Array Challenge 5.1 ', sortedRunners);
8291

83-
// Problem 3
92+
// Problem 3
93+
let divideDonations = runners.map(runner => `${runner.first_name} ${runner.last_name}, half of their donation is ${runner.donation / 2}`);
94+
console.log(divideDonations);

0 commit comments

Comments
 (0)