Skip to content

Commit ab9ac6b

Browse files
committed
MVP: array-methods.js finished. WIP: closures.js
1 parent f6a647e commit ab9ac6b

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

assignments/array-methods.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,59 @@ console.log(ticketPriceTotal);
100100
// 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.
101101

102102
// Problem 1
103+
// Getting the name and contact information of the largest doners. For now above 100 dollars.
104+
let largestDonations = runners.filter(runner => runner.donation >= 100).map(runner => {
105+
return {'first_name': runner.first_name, 'last_name': runner.last_name, 'email': runner.email, 'donation': runner.donation};
106+
});
107+
console.log(largestDonations);
108+
109+
/*
110+
111+
found out that writing arr.map(runner => {key: value}) confusions interpretor.
112+
you need to write object returns in .map() methods as:
113+
114+
arr.map(runner => {
115+
return {key: value};
116+
});
117+
118+
*/
103119

104120
// Problem 2
121+
// How many people wear 'L' T-shirts? With reduce:
122+
let largeCount = runners.reduce((count, currentValue) => {
123+
if (currentValue.shirt_size === 'L') {
124+
count += 1;
125+
return count
126+
}
127+
return count
128+
}, 0)
129+
130+
console.log(largeCount);
131+
// 6 // let's see if this is correct.
132+
133+
let largeCountCheck = runners.filter(runner => {
134+
return runner.shirt_size === 'L';
135+
})
136+
console.log(largeCountCheck.length);
137+
// console.log(largeCountCheck);
138+
// The reduce command was correct the answer was 6 in total.
139+
140+
141+
142+
// Problem 3
143+
// What is the number of donation companies?
144+
145+
/*
146+
147+
Got the following code from:
148+
https://stackoverflow.com/questions/15052702/count-unique-elements-in-array-without-sorting
149+
150+
It uses reduce to find and count the unique values in an array;
151+
152+
*/
105153

106-
// Problem 3
154+
let uniqShirtSizes = runners.map(runner => runner.shirt_size).reduce((acc, val) => {
155+
acc[val] = acc[val] === undefined ? 1 : acc[val] += 1;
156+
return acc;
157+
}, {});
158+
console.log(uniqsShirtSizes);

0 commit comments

Comments
 (0)