Skip to content

Commit e9e9b0d

Browse files
author
Zero
committed
Stretches completed. Final submission
1 parent 7ead011 commit e9e9b0d

3 files changed

Lines changed: 79 additions & 13 deletions

File tree

assignments/array-methods.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,28 @@ console.log(ticketPriceTotal);
8888
// 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.
8989

9090
// Problem 1
91-
91+
// There will be an award ceremony after the marathon is done, we need to find which company/person donated the most.
92+
let mostDonated ='';
93+
let topDonor = [runners[0]];
94+
runners.forEach((runner) => {
95+
if (topDonor.length === 0) { topDonor.push(runner); }
96+
if (runner.donation > topDonor[0].donation) { topDonor.pop() && topDonor.push(runner); }
97+
});
98+
console.log(`${topDonor[0].first_name} from ${topDonor[0].company_name} donated ${topDonor[0].donation}\$!`);
9299
// Problem 2
93-
94-
// Problem 3
100+
// The coordinator of the marathon needs an email list so she can forward an update to all runners.
101+
let emailList = [];
102+
emailList = runners.map(runner => runner.email);
103+
console.log(emailList);
104+
// Problem 3
105+
// The t-shirt manufacturer wants to be able to search up the ID and last name of any competitor so that they
106+
// can easily stencil the ID/Name onto a shirt
107+
idname = [];
108+
runners.forEach((runner) => {
109+
idname.push(`${runner.id} - ${runner.last_name}`);
110+
});
111+
function getIdName(id) {
112+
return idname[id-1];
113+
}
114+
console.log(getIdName(1));
115+
console.log(getIdName(50));

assignments/callbacks.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,24 @@ function contains(item, list, cb) {
6666
}
6767
}
6868

69-
contains('Gum', items, function(bool, item) {
69+
contains('Gum', items, (bool, item) => {
7070
bool == true ? console.log(`${item} has been found`) : console.log(`${item} cannot be found`);
7171
});
7272

7373
/* STRETCH PROBLEM */
7474

75+
const items2 = ['Pencil', 'Notebook', 'yo-yo', 'Gum', 'Gum', 'Pencil', 'Pencil', 'Gum', 'Notebook'];
76+
7577
function removeDuplicates(array, cb) {
76-
// removeDuplicates removes all duplicate values from the given array.
77-
// Pass the duplicate free array to the callback function.
78-
// Do not mutate the original array.
78+
newArray = [];
79+
array.forEach((item) => {
80+
isDuplicate = false;
81+
newArray.includes(item) ? isDuplicate = true : newArray.push(item)
82+
});
83+
return cb(newArray);
7984
}
85+
86+
removeDuplicates(items2, () => {
87+
console.log(newArray);
88+
});
89+

assignments/closure.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,57 @@ closure();
1919
let count = 0;
2020
const counter = () => {
2121
return function() {
22-
return count += 1;
22+
return count ++;
2323
}
2424
// Return a function that when invoked increments and returns a counter variable.
2525
};
26+
2627
newCounter = counter();
2728
console.log(newCounter());
2829
console.log(newCounter());
2930
// Example usage: const newCounter = counter();
3031
// newCounter(); // 1
3132
// newCounter(); // 2
3233

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

35+
36+
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
3537
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
38+
39+
// IIFE Solution
40+
41+
// const counterFactory = ( () => {
42+
// let count = 0
43+
// return {
44+
// 'increment': () => {
45+
// count += 1;
46+
// return count;
47+
// },
48+
// 'decrement': () => {
49+
// count--;
50+
// return count;
51+
// }
52+
// }
53+
// }) ()
54+
55+
// console.log(counterFactory.decrement());
56+
57+
// Solution 2
58+
3659
const counterFactory = () => {
37-
// Return an object that has two methods called `increment` and `decrement`.
38-
// `increment` should increment a counter variable in closure scope and return it.
39-
// `decrement` should decrement the counter variable and return it.
40-
};
60+
let count = 0;
61+
return {
62+
'increment': () => {
63+
count += 1;
64+
return count;
65+
},
66+
'decrement': () => {
67+
count--;
68+
return count;
69+
}
70+
}
71+
}
72+
73+
console.log(counterFactory().increment());
74+
75+

0 commit comments

Comments
 (0)