Skip to content

Commit 7fa2902

Browse files
array-methods.js with one stretch
1 parent d192ca8 commit 7fa2902

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,50 @@ 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(function(element) {
60+
fullName.push(element.first_name + " " + element.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(function(element) {
67+
return element.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(function(element) {
74+
return element.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.map(function(element) {
81+
return element.donation; })
82+
.reduce(function(total, donation) {
83+
return total+donation; });
7484
console.log(ticketPriceTotal);
7585

7686
// ==== Challenge 5: Be Creative ====
7787
// 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.
7888

79-
// Problem 1
89+
// Problem 1 :---- To find out highest donation runner
90+
function maxDonationRunner(arr) {
91+
let max = 0;
92+
let runnerName = "";
93+
runners.forEach(function(element) {
94+
if(max < element.donation) {
95+
max = element.donation;
96+
runnerName = element.first_name + " " + element.last_name;
97+
}
98+
});
99+
return runnerName;
100+
}
101+
102+
console.log(maxDonationRunner(runners)); // invoking funtion maxDonationRunner to find out maximum donation runner from runners.
80103

81104
// Problem 2
82105

0 commit comments

Comments
 (0)