Skip to content

Commit bf28605

Browse files
Merge pull request #1 from Sem8/sem-limi
Sems MVP
2 parents afae86b + 2d32719 commit bf28605

4 files changed

Lines changed: 207 additions & 5 deletions

File tree

‎assignments/array-methods.js‎

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,146 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
5656
// ==== Challenge 1: Use .forEach() ====
5757
// 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.
5858
let fullName = [];
59+
runners.forEach(item => {
60+
var eachName = `${item.first_name}, ${item.last_name}`;
61+
fullName.push(eachName);
62+
return fullName;
63+
});
5964
console.log(fullName);
6065

6166
// ==== Challenge 2: Use .map() ====
6267
// 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
6368
let allCaps = [];
69+
runners.map( eachRunner => {
70+
allCaps.push(eachRunner['first_name'].toUpperCase());
71+
return allCaps;
72+
73+
})
6474
console.log(allCaps);
6575

6676
// ==== Challenge 3: Use .filter() ====
6777
// 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
6878
let largeShirts = [];
79+
let filteredRunners = runners.filter(largeRunners => {
80+
return largeRunners.shirt_size === 'L';
81+
});
82+
largeShirts.push(filteredRunners);
6983
console.log(largeShirts);
7084

7185
// ==== Challenge 4: Use .reduce() ====
7286
// 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
7387
let ticketPriceTotal = [];
88+
let total = runners.reduce((totalMoney, currentRunnerDonation) => {
89+
return totalMoney + currentRunnerDonation.donation;
90+
}, 0);
91+
ticketPriceTotal.push(total);
7492
console.log(ticketPriceTotal);
7593

7694
// ==== Challenge 5: Be Creative ====
7795
// 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.
7896

79-
// Problem 1
97+
// Problem 1 : Total donations from each size shirt,
98+
// will have to use filter to filter out each shirt size. Will have to use reduce to get the total amount.
99+
100+
// Avg. donation from 'xs' runners
101+
let filteredXSRunners = runners.filter(xsRunners => {
102+
return xsRunners.shirt_size === 'XS';
103+
})
104+
let XSRunnerTotalDonation = filteredXSRunners.reduce((allXSRunnerMoney, currentXSRunnerMoney) => {
105+
return allXSRunnerMoney + currentXSRunnerMoney.donation;
106+
}, 0);
107+
// console.log(`${XSRunnerTotalDonation}`);
108+
let XSRunnerAvgDonation = XSRunnerTotalDonation / filteredXSRunners.length;
109+
console.log(`Average Donation from Runners with xs size: ${XSRunnerAvgDonation}`);
110+
111+
// Avg. donation from 's' runners
112+
let filteredSRunners = runners.filter(sRunners => {
113+
return sRunners.shirt_size === 'S';
114+
})
115+
let SRunnerTotalDonation = filteredSRunners.reduce((allSRunnerMoney, currentSRunnerMoney) => {
116+
return allSRunnerMoney + currentSRunnerMoney.donation;
117+
}, 0);
118+
let SRunnerAvgDonation = SRunnerTotalDonation / filteredSRunners.length;
119+
console.log(`Average Donation from Runners with s size: ${SRunnerAvgDonation}`);
120+
121+
// Avg. donation from 'M' runners
122+
let filteredMRunners = runners.filter(MRunners => {
123+
return MRunners.shirt_size === 'M';
124+
})
125+
let MRunnerTotalDonation = filteredMRunners.reduce((allMRunnerMoney, currentMRunnerMoney) => {
126+
return allMRunnerMoney + currentMRunnerMoney.donation;
127+
}, 0);
128+
let MRunnerAvgDonation = MRunnerTotalDonation / filteredMRunners.length;
129+
console.log(`Average Donation from Runners with M size: ${MRunnerAvgDonation}`);
130+
131+
// Avg. donation from 'L' runners
132+
let filteredLRunners = runners.filter(LRunners => {
133+
return LRunners.shirt_size === 'L';
134+
})
135+
let LRunnerTotalDonation = filteredLRunners.reduce((allLRunnerMoney, currentLRunnerMoney) => {
136+
return allLRunnerMoney + currentLRunnerMoney.donation;
137+
}, 0);
138+
let LRunnerAvgDonation = LRunnerTotalDonation / filteredLRunners.length;
139+
console.log(`Average Donation from Runners with L size: ${LRunnerAvgDonation}`);
140+
141+
// Avg. donation from 'XL' runners
142+
let filteredXLRunners = runners.filter(XLRunners => {
143+
return XLRunners.shirt_size === 'XL';
144+
})
145+
let XLRunnerTotalDonation = filteredXLRunners.reduce((allXLRunnerMoney, currentXLRunnerMoney) => {
146+
return allXLRunnerMoney + currentXLRunnerMoney.donation;
147+
}, 0);
148+
let XLRunnerAvgDonation = XLRunnerTotalDonation / filteredXLRunners.length;
149+
console.log(`Average Donation from Runners with XL size: ${XLRunnerAvgDonation}`);
150+
151+
// Avg. donation from 'twoXL' runners
152+
let filteredtwoXLRunners = runners.filter(twoXLRunners => {
153+
return twoXLRunners.shirt_size === '2XL';
154+
})
155+
let twoXLRunnerTotalDonation = filteredtwoXLRunners.reduce((alltwoXLRunnerMoney, currenttwoXLRunnerMoney) => {
156+
return alltwoXLRunnerMoney + currenttwoXLRunnerMoney.donation;
157+
}, 0);
158+
let twoXLRunnerAvgDonation = twoXLRunnerTotalDonation / filteredtwoXLRunners.length;
159+
console.log(`Average Donation from Runners with 2XL size: ${twoXLRunnerAvgDonation}`);
160+
161+
// Avg. donation from 'threeXL' runners
162+
let filteredthreeXLRunners = runners.filter(threeXLRunners => {
163+
return threeXLRunners.shirt_size === '3XL';
164+
})
165+
let threeXLRunnerTotalDonation = filteredthreeXLRunners.reduce((allthreeXLRunnerMoney, currentthreeXLRunnerMoney) => {
166+
return allthreeXLRunnerMoney + currentthreeXLRunnerMoney.donation;
167+
}, 0);
168+
let threeXLRunnerAvgDonation = threeXLRunnerTotalDonation / filteredthreeXLRunners.length;
169+
console.log(`Average Donation from Runners with 3XL size: ${threeXLRunnerAvgDonation}`);
170+
171+
// Conclusion: Medium sized individuals are the stingiest, 3XL sized individuals are the most generous!
172+
173+
174+
// let shirtSizeDonationObj = {
175+
// '3XL': `${threeXLRunnerAvgDonation}`,
176+
// '2XL': `${twoXLRunnerAvgDonation}`
177+
// }
178+
// console.log(shirtSizeDonationObj);
179+
180+
// Problem 2 : Companies with donation of more than $200.
181+
// need to use filter and map;
182+
let highestDonatingCompanies = runners.filter(highestDonatingCompany => {
183+
return highestDonatingCompany.donation > 200;
184+
});
185+
let highestDonatingCompanyNames = highestDonatingCompanies.map(companyName => {
186+
return companyName.company_name;
187+
});
188+
console.log(highestDonatingCompanyNames);
189+
190+
191+
// Problem 3 : find highest donation
80192

81-
// Problem 2
193+
let eachDonor = runners.map(eachPerson => {
194+
return eachPerson['donation'];
195+
});
196+
console.log(eachDonor);
197+
let highestDonation = eachDonor.reduce((previousLargeNumber, currentLargeNumber) => {
198+
return (currentLargeNumber > previousLargeNumber) ? currentLargeNumber : previousLargeNumber;
199+
}, 0);
200+
console.log(highestDonation);
82201

83-
// Problem 3

‎assignments/callbacks.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,66 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2424

2525
function getLength(arr, cb) {
2626
// getLength passes the length of the array into the callback.
27+
return cb(arr.length);
2728
}
29+
getLength(items, length => console.log(length));
30+
2831

2932
function last(arr, cb) {
3033
// last passes the last item of the array into the callback.
34+
return cb(arr[arr.length - 1]);
3135
}
36+
last(items, lastItem => console.log(lastItem));
37+
3238

3339
function sumNums(x, y, cb) {
3440
// sumNums adds two numbers (x, y) and passes the result to the callback.
41+
cb(x, y);
3542
}
43+
const addFunc = (a, b) => console.log(a + b);
44+
sumNums(1, 7, addFunc);
45+
3646

3747
function multiplyNums(x, y, cb) {
3848
// multiplyNums multiplies two numbers and passes the result to the callback.
49+
cb (x, y);
3950
}
51+
const multiplyFunc = (a, b) => console.log(a * b);
52+
multiplyNums(2, 4, multiplyFunc);
53+
4054

4155
function contains(item, list, cb) {
4256
// contains checks if an item is present inside of the given array/list.
4357
// Pass true to the callback if it is, otherwise pass false.
58+
for (let i = 0; i < list.length; i++) {
59+
if(list[i] === item) {
60+
cb();
61+
} else {
62+
return false;
63+
}
64+
}
4465
}
66+
const answerForContains = str => console.log(true);
67+
// contains('Pencil', items, answerForContains);
68+
contains('Smart Phone', items, answerForContains);
4569

4670
/* STRETCH PROBLEM */
4771

72+
const arrayOfDuplicates = ['Onion', 'Garlic', 'Bell Pepper', 'Garlic', 'Ginger', 'Onion', 'Garlic'];
73+
74+
4875
function removeDuplicates(array, cb) {
4976
// removeDuplicates removes all duplicate values from the given array.
5077
// Pass the duplicate free array to the callback function.
5178
// Do not mutate the original array.
79+
let newArr = [];
80+
for (let i = 0; i < array.length; i++) {
81+
if (!newArr.includes(array[i])) {
82+
newArr.push(array[i]);
83+
}
84+
}
85+
cb(newArr);
5286
}
87+
const joiningFunc = anArr => console.log(anArr.join('--'));
88+
removeDuplicates(arrayOfDuplicates, joiningFunc)
89+

‎assignments/closure.js‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
const kingdom = (continentName) => {
4+
console.log(`Welcome to ${continentName}`);
5+
const region = () => {
6+
let continentRegion = 'The North';
7+
console.log(`${continentRegion} in ${continentName} will freeze you over`);
8+
function castle() {
9+
let continentCastle = 'Winterfell'
10+
console.log(`Winter is coming to ${continentCastle} in ${continentRegion} of ${continentName}`);
11+
}
12+
castle();
13+
}
14+
region();
15+
}
16+
kingdom('Westeros');
317

418

519
// ==== Challenge 2: Create a counter function ====
620
const counter = () => {
721
// Return a function that when invoked increments and returns a counter variable.
22+
let count = 0;
23+
return function() {
24+
count++;
25+
return count;
26+
}
827
};
28+
let newCounter = counter();
929
// Example usage: const newCounter = counter();
1030
// newCounter(); // 1
1131
// newCounter(); // 2
32+
console.log(newCounter());
33+
console.log(newCounter());
34+
35+
1236

1337
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
1438

@@ -17,4 +41,18 @@ const counterFactory = () => {
1741
// Return an object that has two methods called `increment` and `decrement`.
1842
// `increment` should increment a counter variable in closure scope and return it.
1943
// `decrement` should decrement the counter variable and return it.
44+
let count = 0;
45+
let counterObj = {
46+
incrementor: function() {
47+
return count++;
48+
},
49+
decrementor: function() {
50+
return count--;
51+
}
52+
}
53+
counterObj.incrementor();
54+
counterObj.decrementor();
55+
2056
};
57+
// console.log(counterFactory());
58+
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax
22

33
// let myFunction = function () {};
4+
let myFunction = () => {};
45

56
// let anotherFunction = function (param) {
67
// return param;
78
// };
9+
let anotherFunction = (param) => param;
10+
console.log(anotherFunction('Hangry'));
811

912
// let add = function (param1, param2) {
1013
// return param1 + param2;
1114
// };
1215
// add(1,2);
16+
let add = (param1, param2) => param1 + param2;
17+
console.log(add(7, 1));
18+
1319

1420
// let subtract = function (param1, param2) {
1521
// return param1 - param2;
1622
// };
1723
// subtract(1,2);
24+
let subtract = (param1, param2) => param1 - param2;
25+
console.log(subtract(17, 9));
1826

19-
// exampleArray = [1,2,3,4];
27+
exampleArray = [1,2,3,4];
2028
// const triple = exampleArray.map(function (num) {
2129
// return num * 3;
2230
// });
23-
// console.log(triple);
31+
const triple = exampleArray.map((num) => num * 3);
32+
console.log(triple);

0 commit comments

Comments
 (0)