Skip to content

Commit ebd26ac

Browse files
committed
Got the challenge. Just had to replace 2XL and 3XL.
1 parent 3dbe48c commit ebd26ac

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,30 @@ console.log(getCountBySize())
108108

109109

110110
// 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
111+
// Create a function getDonationMetricsBySize that has one parameter (size). The size parameter can be "s", "m", "l", "xl", "xxl", etc.
112+
// For a given size, the function should be able to return the min, max, avg and total donations for the given size, via dot notation.
113+
//
113114
// 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-
// TODO get it working for XXL and XXXL
115+
// getDonationMetricsBySize("xl").xl.avg => should give you the average donation for size XL
116+
// getDonationMetricsBySize("m").m.min => should give you the minimum donation for size M
117+
// getDonationMetricsBySize("s").s.max => should give you the maximum donation for size S
118+
// getDonationMetricsBySize("xxxl").xxxl.total => should give you the total donation for size 3XL
119119

120120
function getDonationMetricsBySize(size){
121121

122+
//Make a deep copy
122123
let runnersMod = runners.map( cv => cv);
123-
// if (cv.shirt_size === "2XL") {
124-
// return arr.shirt_size === "XXL"
125-
// }else if (cv.shirt_size === "2XL"){
126-
// return arr.shirt_size === "XXXL"
127-
// }
128-
// console.log(runnersMod.length)
129-
130-
// console.log(runnersMod)
131-
// console.log(runnersMod)
124+
125+
// Remove 2XL and 3XL with XXL and XXXL as they can't be Object keys (starting with numbers).
126+
for (i=0; i<runnersMod.length; i++) {
127+
if (runnersMod[i].shirt_size === "2XL") {
128+
runnersMod[i].shirt_size = "XXL"
129+
}else if (runnersMod[i].shirt_size === "3XL") {
130+
runnersMod[i].shirt_size = "XXXL"
131+
}
132+
}
132133

133-
//Initialize the object
134+
//Initialize the Dontation by Size object
134135
let donateBySize = {
135136
xs : {min:0, max:0,total:0,avg:0},
136137
s : {min:0, max:0,total:0,avg:0},
@@ -141,20 +142,20 @@ function getDonationMetricsBySize(size){
141142
xxxl : {min:0, max:0,total:0,avg:0},
142143
setMin : (arr,size) => donateBySize[size].min = Math.min(...arr),
143144
setMax : (arr,size) => donateBySize[size].max = Math.max(...arr),
144-
setTotal : (arr,size) => donateBySize[size].total = arr.reduce( (acc,cv) => acc + cv),
145+
setTtl : (arr,size) => donateBySize[size].total = arr.reduce( (acc,cv) => acc + cv),
145146
setAvg : (arr,size) => donateBySize[size].avg = arr.reduce( (acc,cv) => acc + cv)/arr.length
146147
}
147148

148149

149150
//Create a function that will return an array of donation for a given size
150151
let filterBySize = function(size) {
151-
return runners.filter( cv => cv.shirt_size === size).map( cv => cv.donation);
152+
return runnersMod.filter( cv => cv.shirt_size === size).map( cv => cv.donation);
152153
}
153154

154155
// Populate each metric by size
155156
donateBySize.setMin(filterBySize(size.toUpperCase()),size);
156157
donateBySize.setMax(filterBySize(size.toUpperCase()),size);
157-
donateBySize.setTotal(filterBySize(size.toUpperCase()),size);
158+
donateBySize.setTtl(filterBySize(size.toUpperCase()),size);
158159
donateBySize.setAvg(filterBySize(size.toUpperCase()),size);
159160

160161
return donateBySize;
@@ -164,7 +165,8 @@ function getDonationMetricsBySize(size){
164165
console.log(getDonationMetricsBySize("xl").xl.avg) //should give you the average donation for size XL
165166
console.log(getDonationMetricsBySize("m").m.min) //should give you the minimum donation for size M
166167
console.log(getDonationMetricsBySize("s").s.max) //should give you the maximum donation for size S
167-
console.log(getDonationMetricsBySize("xl").xl.total) //should give you the total donation for size XL
168+
console.log(getDonationMetricsBySize("xxxl").xxxl.total) //should give you the total donation for size XXXL
168169

169170

170-
// Problem 3
171+
// Problem 3
172+
//That last one took about 5 hours!!!! I"m done.

0 commit comments

Comments
 (0)