Skip to content

Commit 3ffcfaf

Browse files
committed
finished array-methods.js
1 parent 6aa1f08 commit 3ffcfaf

2 files changed

Lines changed: 114 additions & 46 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// 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.
1+
// A local community center is holding a fund-raising 5k fun run and has invited 50 small businesses to make a small donation
2+
// on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the
3+
// event along with a small donation.
24

35
// Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses.
46

@@ -54,30 +56,107 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5456
{"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}];
5557

5658
// ==== Challenge 1: Use .forEach() ====
57-
// 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.
59+
// The event director needs both the first and last names of each runner for their running bibs.
60+
// Combine both the first and last names into a new array called fullName.
5861
let fullName = [];
62+
runners.forEach((element) => {
63+
fullName.push(`${element.first_name} ${element.last_name}`)
64+
})
5965
console.log(fullName);
6066

67+
6168
// ==== Challenge 2: Use .map() ====
62-
// 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
69+
// The event director needs to have all the runner's first names converted to uppercase because the director
70+
// BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
6371
let allCaps = [];
72+
allCaps = runners.map(arrObj => {
73+
return (`${arrObj.first_name.toUpperCase()}`);
74+
});
6475
console.log(allCaps);
6576

77+
6678
// ==== Challenge 3: Use .filter() ====
67-
// 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
79+
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with
80+
// large sized shirts so they can choose a different size. Return an array named largeShirts that contains
81+
// information about the runners that have a shirt size of L and log the result
82+
function isLarge(size) {
83+
return size.shirt_size === 'L';
84+
}
6885
let largeShirts = [];
86+
largeShirts = runners.filter(isLarge);
6987
console.log(largeShirts);
7088

89+
7190
// ==== Challenge 4: Use .reduce() ====
7291
// 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
7392
let ticketPriceTotal = [];
74-
console.log(ticketPriceTotal);
93+
const reducer = (accumulator, currentValue) => accumulator + currentValue;
94+
ticketPriceTotal = runners.map(arrObj => {
95+
return arrObj.donation;
96+
})
97+
console.log(ticketPriceTotal.reduce(reducer));
98+
7599

76100
// ==== Challenge 5: Be Creative ====
77-
// 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.
101+
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential
102+
// problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique
103+
// problems using one or many of the array methods listed above.
78104

79105
// Problem 1
106+
// shirtGate was not handled by the marketing team and now it's up to you to send out emails to each person on the large
107+
// shirt list. Create an array of L shirt people with name and email address
108+
let shirtGate = [];
109+
largeShirts.forEach((element) => {
110+
shirtGate.push({
111+
"id":element.id,
112+
"name":`${element.first_name} ${element.last_name}`,
113+
"email":element.email,
114+
"shirt_size":element.shirt_size,
115+
"replacement_shirt":element.replacement_shirt,
116+
"address":element.address
117+
});
118+
})
119+
console.log(shirtGate);
120+
80121

81122
// Problem 2
123+
// People have responded with their replacement sizes. Add their replacement sizes and mailing address to the array.
124+
// Ehh this kinda doesn't do anything
125+
let shirtResponses = [
126+
{ id: 5,name: 'Gussy Raraty',email: 'graraty4@ucoz.ru',shirt_size: 'L',replacement_shirt: 'XL',address: '90 Circle Path' },
127+
{ id: 17,name: 'Heddie Heningam',email: 'hheningamg@tripadvisor.com',shirt_size: 'L',replacement_shirt: 'XL',address: '50 Hacker Way' },
128+
{ id: 32,name: 'Jessey Walhedd',email: 'jwalheddv@slashdot.org',shirt_size: 'L',replacement_shirt: 'M',address: '1 Infinite Loop' },
129+
{ id: 36,name: 'Tye Manie',email: 'tmaniez@netscape.com',shirt_size: 'L',replacement_shirt: 'XL',address: '333 Broadway' },
130+
{ id: 43,name: 'Hank Zebedee',email: 'hzebedee16@ezinearticles.com',shirt_size: 'L',replacement_shirt: 'M',address: '11 N San Carlos' },
131+
{ id: 46,name: 'Reginauld Purselowe',email: 'rpurselowe19@thetimes.co.uk',shirt_size: 'L',replacement_shirt: 'M',address: '123 Main St' }
132+
]
133+
shirtResponses.forEach((element) => {
134+
shirtGate.push({
135+
"id":element.id,
136+
"name":`${element.name}`,
137+
"email":element.email,
138+
"shirt_size":element.shirt_size,
139+
"replacement_shirt":element.replacement_shirt,
140+
"address":element.address
141+
});
142+
})
143+
console.log(shirtGate);
144+
145+
146+
// Problem 3
147+
// The fun run team would like to setup team tents for runners from the same company.
148+
// If there are more than one person from any company running, add them to a new array called teamTents
149+
let teams = [];
150+
let teamTents = [];
151+
runners.forEach((element) => {
152+
teams.push(` ${element.company_name}`);
153+
})
154+
teams.sort();
155+
for (let i=0; i<teams.length; i++) {
156+
if (teams[i] === teams[i+1]) {
157+
teamTents.push(teams[i]);
158+
}
159+
}
160+
console.log(`Setup team tents for these companies:${teamTents} `);
161+
82162

83-
// Problem 3

‎assignments/callbacks.js‎

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,61 @@
1515

1616
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
1717
const days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
18-
const list = ['Pencil', 'paper', 'bicycle', 'notebook', 'ruler'];
18+
// const list = ['Pencil', 'paper', 'bicycle', 'notebook', 'ruler'];
1919

2020

21+
// Basic output callback function. basicOutput pushes whatever result from the function to the console.
22+
const basicOutput = (len) => { console.log(len); };
2123
// getLength passes the length of the array into the callback.
2224
function getLength(arr, cb) {
23-
return cb(arr.length);
25+
return cb(`The length of the array is "${arr.length}"`);
2426
}
25-
getLength(items, function(len) {
26-
console.log(len);
27-
})
28-
29-
27+
getLength(items, basicOutput);
3028
// last passes the last item of the array into the callback.
3129
function last(arr, cb) {
32-
return cb(arr[arr.length - 1]);
30+
return cb(`The last item in the array is "${arr[arr.length - 1]}"`);
3331
}
34-
last(items, function(lastCB) {
35-
console.log(lastCB);
36-
})
32+
last(items, basicOutput);
3733

3834

3935
// sumNums adds two numbers (x, y) and passes the result to the callback.
4036
function sumNums(x, y, cb) {
41-
return cb(x+y);
37+
return cb(`SUM: ${x + y}`);
4238
}
43-
sumNums(1, 2, function(sumNumsCB) {
44-
console.log(sumNumsCB);
45-
})
46-
47-
39+
sumNums(3, 2, basicOutput);
4840
// multiplyNums multiplies two numbers and passes the result to the callback.
4941
function multiplyNums(x, y, cb) {
50-
return cb(x * y);
42+
return cb(`PRODUCT: ${x * y}`);
5143
}
52-
multiplyNums(3, 9, function(multNumsCB) {
53-
console.log(multNumsCB);
54-
})
44+
multiplyNums(3, 9, basicOutput);
45+
46+
5547

5648
// contains checks if an item is present inside of the given array/list.
5749
// Pass true to the callback if it is, otherwise pass false.
50+
// I think there are quite a few ways I could have done this more efficiently.
5851
function contains(item, list, cb) {
59-
totalArr = [];
60-
console.log(totalArr);
61-
for (let i=0; i < list.length; i++) {
62-
totalArr.push(list[i]);
63-
}
64-
totalArr.push(item);
65-
console.log(totalArr);
66-
totalArr.sort();
67-
console.log(totalArr);
52+
let totalArr = [item.toUpperCase()];
53+
let match = false;
54+
55+
list.forEach((itemInList) => {
56+
totalArr.push(itemInList.toUpperCase());
57+
totalArr.sort();
58+
});
59+
6860
for (let i=0; i < totalArr.length; i++) {
6961
if (totalArr[i] === totalArr[i + 1]) {
70-
console.log(totalArr);
71-
return true;
72-
} else {
73-
return false;
62+
match = true;
7463
}
7564
}
76-
65+
66+
return cb(match);
7767
}
78-
contains('Notebook', items, function(containsCB) {
79-
console.log(containsCB);
80-
})
68+
contains('notebook', items, basicOutput);
69+
8170

82-
/* STRETCH PROBLEM */
8371

72+
/* STRETCH PROBLEM */
8473
function removeDuplicates(array, cb) {
8574
// removeDuplicates removes all duplicate values from the given array.
8675
// Pass the duplicate free array to the callback function.

0 commit comments

Comments
 (0)