Skip to content

Commit 43c60ed

Browse files
Timothy Ross GreyTimothy Ross Grey
authored andcommitted
Finished callbackx, arrays, closures
1 parent f659bf5 commit 43c60ed

4 files changed

Lines changed: 123 additions & 40 deletions

File tree

‎README.md‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,47 @@ With some basic JavaScript principles in hand, we can now expand our skills out
77

88
**Follow these steps to set up and work on your project:**
99

10-
* [ ] Create a forked copy of this project.
11-
* [ ] Add your team lead as collaborator on Github.
12-
* [ ] Clone your OWN version of the repository (Not Lambda's by mistake!).
13-
* [ ] Create a new branch: git checkout -b `<firstName-lastName>`.
14-
* [ ] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
15-
* [ ] Push commits: git push origin `<firstName-lastName>`.
10+
* [X] Create a forked copy of this project.
11+
* [X] Add your team lead as collaborator on Github.
12+
* [X] Clone your OWN version of the repository (Not Lambda's by mistake!).
13+
* [X] Create a new branch: git checkout -b `<firstName-lastName>`.
14+
* [X] Implement the project on your newly created `<firstName-lastName>` branch, committing changes regularly.
15+
* [X] Push commits: git push origin `<firstName-lastName>`.
1616

1717
**Follow these steps for completing your project.**
1818

19-
* [ ] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
20-
* [ ] Add your team lead as a reviewer on the pull-request
19+
* [X] Submit a Pull-Request to merge <firstName-lastName> Branch into master (student's Repo). **Please don't merge your own pull request**
20+
* [X] Add your team lead as a reviewer on the pull-request
2121
* [ ] Your team lead will count the project as complete by merging the branch back into master.
2222

2323
## Task 1: Higher Order Functions and Callbacks
2424

2525
This task focuses on getting practice with higher order functions and callback functions by giving you an array of values and instructions on what to do with that array.
2626

27-
* [ ] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.
27+
* [X] Review the contents of the [callbacks.js](assignments/callbacks.js) file. Notice you are given an array at the top of the page. Use that array to aid you with your functions.
2828

29-
* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
29+
* [X] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
3030

3131
## Task 2: Array Methods
3232

3333
Use `.forEach()`, `.map()`, `.filter()`, and `.reduce()` to loop over an array with 50 objects in it. The [array-methods.js](assignments/array-methods.js) file contains several challenges built around a fundraising 5K fun run event.
3434

35-
* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
35+
* [X] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
3636

37-
* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
37+
* [X] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
3838

39-
* [ ] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
39+
* [X] Notice the last three problems are up to you to create and solve. This is an awesome opportunity for you to push your critical thinking about array methods, have fun with it.
4040

4141
## Task 3: Closures
4242

4343
We have learned that closures allow us to access values in scope that have already been invoked (lexical scope).
4444

4545
**Hint: Utilize debugger statements in your code in combination with your developer tools to easily identify closure values.**
4646

47-
* [ ] Review the contents of the [closure.js](assignments/closure.js) file.
48-
* [ ] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
47+
* [X] Review the contents of the [closure.js](assignments/closure.js) file.
48+
* [X] Complete the problems provided to you but skip over stretch problems until you are complete with every other JS file first.
4949

5050
## Stretch Goals
5151

52-
* [ ] Go back through the stretch problems that you skipped over and complete as many as you can.
53-
* [ ] Look up what an IIFE is in JavaScript and experiment with them
52+
* [X] Go back through the stretch problems that you skipped over and complete as many as you can.
53+
* [X] Look up what an IIFE is in JavaScript and experiment with them

‎assignments/array-methods.js‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,42 @@ const runners = [
5757

5858
// ==== Challenge 1: Use .forEach() ====
5959
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names and populate a new array called `fullNames`. This array will contain just strings.
60-
let fullNames = [];
61-
console.log(fullNames);
60+
// let fullNames = [];
61+
// console.log(fullNames);
6262

63+
runners.forEach((fullNames)=> {
64+
console.log(`${fullNames.first_name} ${fullNames.last_name}`)
65+
})
66+
console.log(fullNames);
67+
// -------------------------------------------------------------
6368
// ==== Challenge 2: Use .map() ====
6469
// The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings.
6570
let firstNamesAllCaps = [];
66-
console.log(firstNamesAllCaps);
71+
72+
runners.map((firstNamesAllCaps)=>{
73+
console.log(`${firstNamesAllCaps.first_name.toUpperCase()}`)
74+
});
75+
76+
77+
// -------------------------------------------------------------
6778

6879
// ==== Challenge 3: Use .filter() ====
6980
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects.
70-
let runnersLargeSizeShirt = [];
71-
console.log(runnersLargeSizeShirt);
81+
82+
const runnersLargeSizeShirt = runners.filter((item) =>{
83+
return item.shirt_size === "L"
84+
})
85+
console.log(runnersLargeSizeShirt)
86+
// -------------------------------------------------------------
7287

7388
// ==== Challenge 4: Use .reduce() ====
7489
// The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable.
75-
let ticketPriceTotal = 0;
90+
// let ticketPriceTotal = 0;
91+
// console.log(ticketPriceTotal);
92+
93+
const ticketPriceTotal= runners.reduce((currentTotal, item) => {
94+
return item.donation + currentTotal
95+
}, 0)
7696
console.log(ticketPriceTotal);
7797

7898
// ==== Challenge 5: Be Creative ====

‎assignments/callbacks.js‎

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,90 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
3939
*/
4040

4141

42-
function getLength(arr, cb) {
42+
// function getLength(arr, cb) {
4343
// getLength passes the length of the array into the callback.
44+
// }
45+
const getLength = (arr, cb) => {
46+
cb(arr.length);
47+
};
48+
getLength(items, length => {
49+
console.log(`The length of the array is ${length}.`);
50+
});
51+
// ------------------------------------------
52+
// function last(arr, cb) {
53+
// last passes the last item of the array into the callback.
54+
// }
55+
56+
const last = (arr, cb) => {
57+
cb(arr[arr.length -1])
4458
}
59+
last(items, lastItem => {
60+
console.log(`The last item in the array is ${lastItem}`)
61+
});
4562

46-
function last(arr, cb) {
47-
// last passes the last item of the array into the callback.
48-
}
4963

50-
function sumNums(x, y, cb) {
51-
// sumNums adds two numbers (x, y) and passes the result to the callback.
52-
}
5364

54-
function multiplyNums(x, y, cb) {
55-
// multiplyNums multiplies two numbers and passes the result to the callback.
56-
}
65+
// -----------------------------------------------
66+
// function sumNums(x, y, cb) {
67+
// // sumNums adds two numbers (x, y) and passes the result to the callback.
68+
// }
69+
70+
const sumsNums= (x, y, cb) => {
71+
cb(x + y);
72+
};
73+
74+
sumsNums(10,11, sum => {
75+
console.log(sum);
76+
});
77+
78+
79+
80+
// -----------------------------------------------
81+
// function multiplyNums(x, y, cb) {
82+
// // multiplyNums multiplies two numbers and passes the result to the callback.
83+
// }
84+
const multiplyNums=(x, y, cb)=> {
85+
cb(x * y);
86+
};
87+
//
88+
multiplyNums(10, 11, product=> {
89+
console.log(`The answer is ${product}`);
90+
});
91+
92+
// ------------------------------------------------
93+
// function contains(item, list, cb) {
94+
// contains checks if an item is present inside of the given array/list.
95+
// // Pass true to the callback if it is, otherwise pass false.
96+
// }
97+
function contains(item,list,cb){
98+
if(list.includes(item)){
99+
return cb(true)
100+
}
101+
return cb(false)
102+
103+
104+
}
105+
106+
contains(3,[1,2,3],function(success){
107+
if(success){
108+
return "item is contained in the list"
109+
}
110+
return "item is not present in the list"
111+
})
112+
57113

58-
function contains(item, list, cb) {
59-
// contains checks if an item is present inside of the given array/list.
60-
// Pass true to the callback if it is, otherwise pass false.
61-
}
62114

63115
/* STRETCH PROBLEM */
64116

65-
function removeDuplicates(array, cb) {
117+
// function removeDuplicates(array, cb) {
66118
// removeDuplicates removes all duplicate values from the given array.
67119
// Pass the duplicate free array to the callback function.
68120
// Do not mutate the original array.
69-
}
121+
// }
122+
const removeDuplicates = (arr, cb) => {
123+
var mySet = new Set(arr);
124+
cb([...mySet]);
125+
};
126+
removeDuplicates(foods, uniqueFoods => {
127+
console.log(`foods with duplicates removed: ${uniqueFoods}`);
128+
});

‎assignments/closure.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
// that manipulates variables defined in the outer scope.
55
// The outer scope can be a parent function, or the top level of the script.
66

7-
7+
let pooperScooper = "Drone service that will pick up your doggies poop and drop it into your neighbors yard";
8+
function whatIsPooperScooper(){
9+
return pooperScooper;
10+
}
11+
console.log(whatIsPooperScooper());
812
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
913

1014

0 commit comments

Comments
 (0)