Skip to content

Commit c0d1bf4

Browse files
committed
Callbacks and Closure done
1 parent fe1d958 commit c0d1bf4

3 files changed

Lines changed: 80 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ This task focuses on getting practice with callback functions by giving you an a
2525

2626
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.
2727

28-
* [ ] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
28+
* [x] Review the contents of the [array-methods.js](assignments/array-methods.js) file.
2929

30-
* [ ] Complete the problems provided to you
30+
* [x] Complete the problems provided to you
3131

32-
* [ ] 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.
32+
* [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.
3333

3434
## Task 4: Closures
3535

assignments/callbacks.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,72 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2121
2222
*/
2323

24+
function firstItem(arr, cb) {
25+
return cb(arr[0]);
26+
}
27+
28+
firstItem(items, (item) => {
29+
console.log(item);
30+
});
31+
32+
2433

2534
function getLength(arr, cb) {
2635
// getLength passes the length of the array into the callback.
36+
cb(arr.length);
2737
}
2838

39+
getLength(items, (lengthOfList) => {
40+
console.log(lengthOfList);
41+
} )
42+
2943
function last(arr, cb) {
3044
// last passes the last item of the array into the callback.
45+
return cb(arr.slice(-1)[0] );
3146
}
3247

48+
last(items, (lastOfList) => {
49+
console.log(lastOfList);
50+
});
51+
3352
function sumNums(x, y, cb) {
3453
// sumNums adds two numbers (x, y) and passes the result to the callback.
54+
cb(x,y);
55+
3556
}
3657

58+
function sum(a,b) {
59+
console.log(a+b);
60+
}
61+
62+
sumNums(5,6,sum);
63+
3764
function multiplyNums(x, y, cb) {
3865
// multiplyNums multiplies two numbers and passes the result to the callback.
66+
cb(x,y);
3967
}
4068

69+
function multiply(a,b) {
70+
console.log(a*b);
71+
}
72+
73+
multiplyNums(5,6,multiply);
74+
75+
4176
function contains(item, list, cb) {
4277
// contains checks if an item is present inside of the given array/list.
4378
// Pass true to the callback if it is, otherwise pass false.
44-
}
79+
80+
if(list.includes(item)){
81+
return cb(true);
82+
} else {
83+
return cb(false);
84+
}
85+
}
86+
87+
contains('Gum', items, function(doYouHaveThis){
88+
console.log(doYouHaveThis);
89+
});
4590

4691
/* STRETCH PROBLEM */
4792

assignments/closure.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
// Write a simple closure of your own creation. Keep it simple!
33

44

5+
function nameLastName (firstName, lastName) {
6+
const startingName = "My name is";
7+
8+
function makeFullName () {
9+
return (`${startingName} ${firstName} ${lastName}`);
10+
}
11+
return makeFullName ();
12+
}
13+
14+
console.log(nameLastName ("Julian", "Moreno"));
15+
16+
17+
518
// ==== Challenge 2: Create a counter function ====
619
const counter = () => {
720
// Return a function that when invoked increments and returns a counter variable.
@@ -10,6 +23,24 @@ const counter = () => {
1023
// newCounter(); // 1
1124
// newCounter(); // 2
1225

26+
const todaysCoffees = () => {
27+
let coffee = 0;
28+
29+
return function() {
30+
return ++coffee;
31+
}
32+
};
33+
34+
const newTodaysCofees = todaysCoffees();
35+
console.log(newTodaysCofees() + " ");
36+
console.log(newTodaysCofees() + " One more");
37+
console.log(newTodaysCofees() + " One more");
38+
console.log(newTodaysCofees() + " One more");
39+
console.log(newTodaysCofees() + " One more");
40+
console.log(newTodaysCofees() + " One more");
41+
42+
43+
1344
/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
1445

1546
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====

0 commit comments

Comments
 (0)