Skip to content

Commit 6be3395

Browse files
committed
project completed
1 parent bc4a039 commit 6be3395

File tree

3 files changed

+67
-26
lines changed

3 files changed

+67
-26
lines changed

assignments/array-methods.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// A local community center is holding a fund raising 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 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.
22

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

@@ -56,28 +56,31 @@ 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((currentValue) => {
60+
fullName.push(`${currentValue.first_name} ${currentValue.last_name}`);
61+
})
5962
console.log(fullName);
6063

6164
// ==== Challenge 2: Use .map() ====
6265
// 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
6366
let allCaps = [];
67+
allCaps = runners.map(function(currentValue){
68+
return currentValue.first_name.toUpperCase();
69+
})
6470
console.log(allCaps);
6571

6672
// ==== Challenge 3: Use .filter() ====
6773
// 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
6874
let largeShirts = [];
75+
largeShirts = runners.filter(function(currentValue){
76+
return currentValue.shirt_size === "L";
77+
})
6978
console.log(largeShirts);
7079

7180
// ==== Challenge 4: Use .reduce() ====
7281
// 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
7382
let ticketPriceTotal = [];
74-
console.log(ticketPriceTotal);
75-
76-
// ==== 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.
78-
79-
// Problem 1
80-
81-
// Problem 2
82-
83-
// Problem 3
83+
ticketPriceTotal.push(runners.reduce(function(total, currentValue){
84+
return total + currentValue.donation;
85+
}, 0))
86+
console.log(ticketPriceTotal);

assignments/callbacks.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,70 @@
1-
// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
1+
// Create a callback function and invoke the function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems.
22

33
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
44

55
/*
6-
76
//Given this problem:
87
98
function firstItem(arr, cb) {
109
// firstItem passes the first item of the given array to the callback function.
1110
}
12-
1311
// Potential Solution:
14-
15-
// Higher order function using "cb" as the call back
1612
function firstItem(arr, cb) {
1713
return cb(arr[0]);
1814
}
19-
20-
// Function invocation
2115
firstItem(items, function(first) {
2216
console.log(first)
2317
});
24-
2518
*/
2619

2720

2821
function getLength(arr, cb) {
2922
// getLength passes the length of the array into the callback.
23+
return cb(arr.length);
3024
}
3125

3226
function last(arr, cb) {
3327
// last passes the last item of the array into the callback.
28+
return cb(arr[arr.length-1]);
3429
}
3530

3631
function sumNums(x, y, cb) {
3732
// sumNums adds two numbers (x, y) and passes the result to the callback.
33+
return cb(x + y);
3834
}
3935

4036
function multiplyNums(x, y, cb) {
4137
// multiplyNums multiplies two numbers and passes the result to the callback.
38+
return cb(x * y);
4239
}
4340

4441
function contains(item, list, cb) {
4542
// contains checks if an item is present inside of the given array/list.
4643
// Pass true to the callback if it is, otherwise pass false.
44+
let placeHolder = [];
45+
for (let i=0; i<list.length; i++){
46+
if (item === list[i]){
47+
placeHolder.push(list[i]);
48+
}
49+
}
50+
if (!placeHolder[0]){
51+
return cb(false);
52+
} else {
53+
return cb(true);
54+
}
4755
}
4856

57+
// --ES6--
58+
// function contains(item, list, cb) {
59+
// cb(list.includes(item));
60+
// }
61+
62+
contains('Pencil', items, (cb) => (console.log(cb)));
63+
4964
/* STRETCH PROBLEM */
5065

5166
function removeDuplicates(array, cb) {
5267
// removeDuplicates removes all duplicate values from the given array.
5368
// Pass the duplicate free array to the callback function.
5469
// Do not mutate the original array.
55-
}
70+
}

assignments/closure.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3+
const number = 5;
34

5+
function first (number) {
6+
const math = number;
7+
console.log(math);
8+
//debugger;
49

5-
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
10+
function second() {
11+
const moreMath = 10;
12+
console.log(moreMath + math);
13+
//debugger;
14+
15+
function third() {
16+
const moreMoreMath = 15;
17+
console.log(moreMath + moreMoreMath + math);
18+
//debugger;
19+
}
20+
third();
21+
}
22+
second();
23+
}
24+
first();
625

726

827
// ==== Challenge 2: Create a counter function ====
28+
let accumulator = 0;
929
const counter = () => {
1030
// Return a function that when invoked increments and returns a counter variable.
31+
console.log(accumulator++);
32+
1133
};
34+
counter();
35+
counter();
36+
counter();
1237
// Example usage: const newCounter = counter();
1338
// newCounter(); // 1
1439
// newCounter(); // 2
1540

41+
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
42+
1643
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
17-
const counterFactory = () => {
18-
// Return an object that has two methods called `increment` and `decrement`.
19-
// `increment` should increment a counter variable in closure scope and return it.
20-
// `decrement` should decrement the counter variable and return it.
21-
};
44+

0 commit comments

Comments
 (0)