forked from bloominstituteoftechnology/JavaScript-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-methods.js
More file actions
201 lines (177 loc) · 13.8 KB
/
Copy patharray-methods.js
File metadata and controls
201 lines (177 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// A local community center is holding a fund rasising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation.
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75},
{"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"[email protected]","shirt_size":"2XL","company_name":"Wordtune","donation":148},
{"id":3,"first_name":"Julieta","last_name":"McCloid","email":"[email protected]","shirt_size":"S","company_name":"Riffpedia","donation":171},
{"id":4,"first_name":"Martynne","last_name":"Paye","email":"[email protected]","shirt_size":"XL","company_name":"Wordware","donation":288},
{"id":5,"first_name":"Gussy","last_name":"Raraty","email":"[email protected]","shirt_size":"L","company_name":"Oozz","donation":291},
{"id":6,"first_name":"Yule","last_name":"Tommasetti","email":"[email protected]","shirt_size":"S","company_name":"Yodo","donation":27},
{"id":7,"first_name":"Kathie","last_name":"Majury","email":"[email protected]","shirt_size":"3XL","company_name":"Zoomcast","donation":261},
{"id":8,"first_name":"Tanner","last_name":"Branton","email":"[email protected]","shirt_size":"2XL","company_name":"Realmix","donation":28},
{"id":9,"first_name":"Sarina","last_name":"Lasham","email":"[email protected]","shirt_size":"XL","company_name":"Gigashots","donation":110},
{"id":10,"first_name":"Bertie","last_name":"Lonergan","email":"[email protected]","shirt_size":"3XL","company_name":"Skinte","donation":62},
{"id":11,"first_name":"Trevor","last_name":"Studd","email":"[email protected]","shirt_size":"S","company_name":"Cogidoo","donation":76},
{"id":12,"first_name":"Malachi","last_name":"Okeshott","email":"[email protected]","shirt_size":"M","company_name":"DabZ","donation":91},
{"id":13,"first_name":"Berget","last_name":"Logsdail","email":"[email protected]","shirt_size":"M","company_name":"Mymm","donation":9},
{"id":14,"first_name":"Loise","last_name":"Rivlin","email":"[email protected]","shirt_size":"2XL","company_name":"Linktype","donation":204},
{"id":15,"first_name":"Christan","last_name":"Kendall","email":"[email protected]","shirt_size":"XS","company_name":"Skinix","donation":252},
{"id":16,"first_name":"Kayla","last_name":"Whitwam","email":"[email protected]","shirt_size":"2XL","company_name":"Oyope","donation":147},
{"id":17,"first_name":"Heddie","last_name":"Heningam","email":"[email protected]","shirt_size":"L","company_name":"Skinix","donation":172},
{"id":18,"first_name":"Mace","last_name":"Ballinger","email":"[email protected]","shirt_size":"S","company_name":"Nlounge","donation":266},
{"id":19,"first_name":"Nola","last_name":"Abberley","email":"[email protected]","shirt_size":"XL","company_name":"Photospace","donation":148},
{"id":20,"first_name":"Nadine","last_name":"Tresler","email":"[email protected]","shirt_size":"3XL","company_name":"Wikido","donation":292},
{"id":21,"first_name":"Ulrikaumeko","last_name":"Vuittet","email":"[email protected]","shirt_size":"S","company_name":"Skinte","donation":102},
{"id":22,"first_name":"Saunder","last_name":"Spennock","email":"[email protected]","shirt_size":"3XL","company_name":"Kwimbee","donation":213},
{"id":23,"first_name":"Carmel","last_name":"Woffinden","email":"[email protected]","shirt_size":"S","company_name":"Rooxo","donation":137},
{"id":24,"first_name":"Marielle","last_name":"Kimmel","email":"[email protected]","shirt_size":"M","company_name":"Livetube","donation":96},
{"id":25,"first_name":"Brucie","last_name":"Burris","email":"[email protected]","shirt_size":"2XL","company_name":"Wordtune","donation":128},
{"id":26,"first_name":"Juan","last_name":"Berzon","email":"[email protected]","shirt_size":"3XL","company_name":"Einti","donation":234},
{"id":27,"first_name":"Sacha","last_name":"Olsen","email":"[email protected]","shirt_size":"2XL","company_name":"Viva","donation":190},
{"id":28,"first_name":"Jamey","last_name":"O'Nolan","email":"[email protected]","shirt_size":"XL","company_name":"Skinix","donation":31},
{"id":29,"first_name":"Morrie","last_name":"Rainard","email":"[email protected]","shirt_size":"XS","company_name":"Podcat","donation":52},
{"id":30,"first_name":"Fidel","last_name":"Roskelly","email":"[email protected]","shirt_size":"XS","company_name":"Avavee","donation":5},
{"id":31,"first_name":"Toni","last_name":"MacSweeney","email":"[email protected]","shirt_size":"M","company_name":"Jaloo","donation":82},
{"id":32,"first_name":"Jessey","last_name":"Walhedd","email":"[email protected]","shirt_size":"L","company_name":"Trilia","donation":5},
{"id":33,"first_name":"Karola","last_name":"Piper","email":"[email protected]","shirt_size":"3XL","company_name":"Yombu","donation":110},
{"id":34,"first_name":"Marley","last_name":"Mitchenson","email":"[email protected]","shirt_size":"M","company_name":"Zoonoodle","donation":97},
{"id":35,"first_name":"Marrilee","last_name":"Thrasher","email":"[email protected]","shirt_size":"XL","company_name":"Bluejam","donation":17},
{"id":36,"first_name":"Tye","last_name":"Manie","email":"[email protected]","shirt_size":"L","company_name":"Kanoodle","donation":30},
{"id":37,"first_name":"Charleen","last_name":"Sheering","email":"[email protected]","shirt_size":"3XL","company_name":"Jatri","donation":262},
{"id":38,"first_name":"Valma","last_name":"Eynaud","email":"[email protected]","shirt_size":"XS","company_name":"Jaxbean","donation":212},
{"id":39,"first_name":"Dollie","last_name":"McDarmid","email":"[email protected]","shirt_size":"S","company_name":"Kayveo","donation":74},
{"id":40,"first_name":"Minna","last_name":"Hymas","email":"[email protected]","shirt_size":"XS","company_name":"Vimbo","donation":101},
{"id":41,"first_name":"Jsandye","last_name":"Frend","email":"[email protected]","shirt_size":"XS","company_name":"Latz","donation":156},
{"id":42,"first_name":"Yevette","last_name":"Hacket","email":"[email protected]","shirt_size":"XL","company_name":"Lazzy","donation":291},
{"id":43,"first_name":"Hank","last_name":"Zebedee","email":"[email protected]","shirt_size":"L","company_name":"Gigashots","donation":241},
{"id":44,"first_name":"Jodie","last_name":"Stawell","email":"[email protected]","shirt_size":"S","company_name":"Jaxspan","donation":262},
{"id":45,"first_name":"Falito","last_name":"Karsh","email":"[email protected]","shirt_size":"S","company_name":"Mycat","donation":239},
{"id":46,"first_name":"Reginauld","last_name":"Purselowe","email":"[email protected]","shirt_size":"L","company_name":"Jabbersphere","donation":11},
{"id":47,"first_name":"Vida","last_name":"Tydd","email":"[email protected]","shirt_size":"S","company_name":"Quaxo","donation":55},
{"id":48,"first_name":"Anderea","last_name":"MacGiolla Pheadair","email":"[email protected]","shirt_size":"2XL","company_name":"Kwimbee","donation":214},
{"id":49,"first_name":"Bel","last_name":"Alway","email":"[email protected]","shirt_size":"S","company_name":"Voolia","donation":107},
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];
// ==== Challenge 1: Use .forEach() ====
// 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.
let fullName = [];
runners.forEach(item => {
var eachName = `${item.first_name}, ${item.last_name}`;
fullName.push(eachName);
return fullName;
});
console.log(fullName);
// ==== Challenge 2: Use .map() ====
// 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
let allCaps = [];
runners.map( eachRunner => {
allCaps.push(eachRunner['first_name'].toUpperCase());
return allCaps;
})
console.log(allCaps);
// ==== Challenge 3: Use .filter() ====
// 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
let largeShirts = [];
let filteredRunners = runners.filter(largeRunners => {
return largeRunners.shirt_size === 'L';
});
largeShirts.push(filteredRunners);
console.log(largeShirts);
// ==== Challenge 4: Use .reduce() ====
// 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
let ticketPriceTotal = [];
let total = runners.reduce((totalMoney, currentRunnerDonation) => {
return totalMoney + currentRunnerDonation.donation;
}, 0);
ticketPriceTotal.push(total);
console.log(ticketPriceTotal);
// ==== Challenge 5: Be Creative ====
// 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.
// Problem 1 : Total donations from each size shirt,
// will have to use filter to filter out each shirt size. Will have to use reduce to get the total amount.
// Avg. donation from 'xs' runners
let filteredXSRunners = runners.filter(xsRunners => {
return xsRunners.shirt_size === 'XS';
})
let XSRunnerTotalDonation = filteredXSRunners.reduce((allXSRunnerMoney, currentXSRunnerMoney) => {
return allXSRunnerMoney + currentXSRunnerMoney.donation;
}, 0);
// console.log(`${XSRunnerTotalDonation}`);
let XSRunnerAvgDonation = XSRunnerTotalDonation / filteredXSRunners.length;
console.log(`Average Donation from Runners with xs size: ${XSRunnerAvgDonation}`);
// Avg. donation from 's' runners
let filteredSRunners = runners.filter(sRunners => {
return sRunners.shirt_size === 'S';
})
let SRunnerTotalDonation = filteredSRunners.reduce((allSRunnerMoney, currentSRunnerMoney) => {
return allSRunnerMoney + currentSRunnerMoney.donation;
}, 0);
let SRunnerAvgDonation = SRunnerTotalDonation / filteredSRunners.length;
console.log(`Average Donation from Runners with s size: ${SRunnerAvgDonation}`);
// Avg. donation from 'M' runners
let filteredMRunners = runners.filter(MRunners => {
return MRunners.shirt_size === 'M';
})
let MRunnerTotalDonation = filteredMRunners.reduce((allMRunnerMoney, currentMRunnerMoney) => {
return allMRunnerMoney + currentMRunnerMoney.donation;
}, 0);
let MRunnerAvgDonation = MRunnerTotalDonation / filteredMRunners.length;
console.log(`Average Donation from Runners with M size: ${MRunnerAvgDonation}`);
// Avg. donation from 'L' runners
let filteredLRunners = runners.filter(LRunners => {
return LRunners.shirt_size === 'L';
})
let LRunnerTotalDonation = filteredLRunners.reduce((allLRunnerMoney, currentLRunnerMoney) => {
return allLRunnerMoney + currentLRunnerMoney.donation;
}, 0);
let LRunnerAvgDonation = LRunnerTotalDonation / filteredLRunners.length;
console.log(`Average Donation from Runners with L size: ${LRunnerAvgDonation}`);
// Avg. donation from 'XL' runners
let filteredXLRunners = runners.filter(XLRunners => {
return XLRunners.shirt_size === 'XL';
})
let XLRunnerTotalDonation = filteredXLRunners.reduce((allXLRunnerMoney, currentXLRunnerMoney) => {
return allXLRunnerMoney + currentXLRunnerMoney.donation;
}, 0);
let XLRunnerAvgDonation = XLRunnerTotalDonation / filteredXLRunners.length;
console.log(`Average Donation from Runners with XL size: ${XLRunnerAvgDonation}`);
// Avg. donation from 'twoXL' runners
let filteredtwoXLRunners = runners.filter(twoXLRunners => {
return twoXLRunners.shirt_size === '2XL';
})
let twoXLRunnerTotalDonation = filteredtwoXLRunners.reduce((alltwoXLRunnerMoney, currenttwoXLRunnerMoney) => {
return alltwoXLRunnerMoney + currenttwoXLRunnerMoney.donation;
}, 0);
let twoXLRunnerAvgDonation = twoXLRunnerTotalDonation / filteredtwoXLRunners.length;
console.log(`Average Donation from Runners with 2XL size: ${twoXLRunnerAvgDonation}`);
// Avg. donation from 'threeXL' runners
let filteredthreeXLRunners = runners.filter(threeXLRunners => {
return threeXLRunners.shirt_size === '3XL';
})
let threeXLRunnerTotalDonation = filteredthreeXLRunners.reduce((allthreeXLRunnerMoney, currentthreeXLRunnerMoney) => {
return allthreeXLRunnerMoney + currentthreeXLRunnerMoney.donation;
}, 0);
let threeXLRunnerAvgDonation = threeXLRunnerTotalDonation / filteredthreeXLRunners.length;
console.log(`Average Donation from Runners with 3XL size: ${threeXLRunnerAvgDonation}`);
// Conclusion: Medium sized individuals are the stingiest, 3XL sized individuals are the most generous!
// let shirtSizeDonationObj = {
// '3XL': `${threeXLRunnerAvgDonation}`,
// '2XL': `${twoXLRunnerAvgDonation}`
// }
// console.log(shirtSizeDonationObj);
// Problem 2 : Companies with donation of more than $200.
// need to use filter and map;
let highestDonatingCompanies = runners.filter(highestDonatingCompany => {
return highestDonatingCompany.donation > 200;
});
let highestDonatingCompanyNames = highestDonatingCompanies.map(companyName => {
return companyName.company_name;
});
console.log(highestDonatingCompanyNames);
// Problem 3 : find highest donation
let eachDonor = runners.map(eachPerson => {
return eachPerson['donation'];
});
console.log(eachDonor);
let highestDonation = eachDonor.reduce((previousLargeNumber, currentLargeNumber) => {
return (currentLargeNumber > previousLargeNumber) ? currentLargeNumber : previousLargeNumber;
}, 0);
console.log(highestDonation);