Skip to content

Commit a7b4f17

Browse files
committed
almost done MVP
1 parent afae86b commit a7b4f17

2 files changed

Lines changed: 67 additions & 1 deletion

File tree

‎assignments/array-methods.js‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,56 @@ 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+
let filteredXSRunners = runners.filter(xsRunners => {
100+
return xsRunners.shirt_size === 'XS';
101+
})
102+
let XSRunnerTotalDonation = filteredXSRunners.reduce((allXSRunnerMoney, currentXSRunnerMoney) => {
103+
return allXSRunnerMoney + currentXSRunnerMoney.donation;
104+
}, 0);
105+
// console.log(`${XSRunnerTotalDonation}`);
106+
let XSRunnerAvgDonation = XSRunnerTotalDonation / filteredXSRunners.length;
107+
console.log(XSRunnerAvgDonation);
108+
80109

81110
// Problem 2
82111

‎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+

0 commit comments

Comments
 (0)