Skip to content

Commit 950fa05

Browse files
committed
Tried the challenge
1 parent 34084dd commit 950fa05

1 file changed

Lines changed: 84 additions & 1 deletion

File tree

‎assignments/array-methods.js‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,94 @@ console.log(largeShirts);
7474
let ticketPriceTotal = runners.reduce( (acc,cv) => acc += cv.donation, 0);
7575
console.log(ticketPriceTotal);
7676

77+
console.table(runners)
78+
79+
80+
7781
// ==== Challenge 5: Be Creative ====
7882
// 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.
7983

80-
// Problem 1
84+
// Problem 1 - For Each, closure, objects, etc...
85+
// Create an function that returns an object that has the {key :value} pairs as the {size : total number of orders in that size}
86+
function getCountBySize(){
87+
88+
//Put all valid sizes in an array
89+
let sizeArr = ["XS","S","M","L","XL","2XL","3XL"];
90+
91+
//Create method that increments any size property, parametrically.
92+
let sizeCount = {
93+
increment : size => ++sizeCount[size]
94+
};
95+
96+
//Initialize all size properties in the object to 0
97+
sizeArr.forEach( cv => sizeCount[cv] = 0)
98+
99+
//Loop through each value in runners, compare with each value in sizeArr, when a match is found, increment that size.
100+
runners.forEach( cvRun => {
101+
sizeArr.forEach( cvSize => {
102+
if (cvRun.shirt_size === cvSize) sizeCount.increment(cvSize);
103+
})
104+
})
105+
return sizeCount;
106+
}
107+
console.log(getCountBySize())
108+
81109

82110
// Problem 2
111+
// Create a function getDonationMetricsBySize, For each size, find the min, max, avg and total donations and store into an object that can get any value via dot
112+
// notation
113+
// For example
114+
// getDonationMetricsBySize("xl").xl.avg should give you the average donation for size XL
115+
// getDonationMetricsBySize("m").m.min should give you the minimum donation for size M
116+
// getDonationMetricsBySize("s").s.max should give you the maximum donation for size S
117+
// getDonationMetricsBySize("xxxl").xxxl.total should give you the total donation for size XXXL
118+
119+
function getDonationMetricsBySize(size){
120+
121+
let runnersMod = runners.map((cv,ci,arr) => {
122+
if (cv.shirt_size === "2XL") {
123+
return arr.shirt_size === "XXL"
124+
}else if (cv.shirt_size === "2XL"){
125+
return arr.shirt_size === "XXXL"
126+
}
127+
})
128+
console.log(runnersMod)
129+
130+
//Initialize the object
131+
let donateBySize = {
132+
xs : {min:0, max:0,total:0,avg:0},
133+
s : {min:0, max:0,total:0,avg:0},
134+
m : {min:0, max:0,total:0,avg:0},
135+
l : {min:0, max:0,total:0,avg:0},
136+
xl : {min:0, max:0,total:0,avg:0},
137+
xxl : {min:0, max:0,total:0,avg:0},
138+
xxxl : {min:0, max:0,total:0,avg:0},
139+
setMin : (arr,size) => donateBySize[size].min = Math.min(...arr),
140+
setMax : (arr,size) => donateBySize[size].max = Math.max(...arr),
141+
setTotal : (arr,size) => donateBySize[size].total = arr.reduce( (acc,cv) => acc + cv),
142+
setAvg : (arr,size) => donateBySize[size].avg = arr.reduce( (acc,cv) => acc + cv)/arr.length
143+
}
144+
145+
146+
//Create a function that will return an array of donation for a given size
147+
let filterBySize = function(size) {
148+
return runners.filter( cv => cv.shirt_size === size).map( cv => cv.donation);
149+
}
150+
151+
// Populate each metric by size
152+
donateBySize.setMin(filterBySize(size.toUpperCase()),size);
153+
donateBySize.setMax(filterBySize(size.toUpperCase()),size);
154+
donateBySize.setTotal(filterBySize(size.toUpperCase()),size);
155+
donateBySize.setAvg(filterBySize(size.toUpperCase()),size);
156+
157+
return donateBySize;
158+
159+
}
160+
161+
console.log(getDonationMetricsBySize("xl").xl.avg) //should give you the average donation for size XL
162+
console.log(getDonationMetricsBySize("m").m.min) //should give you the minimum donation for size M
163+
console.log(getDonationMetricsBySize("s").s.max) //should give you the maximum donation for size S
164+
console.log(getDonationMetricsBySize("xxxl").xxxl.total) //should give you the total donation for size XL
165+
83166

84167
// Problem 3

0 commit comments

Comments
 (0)