Skip to content

Commit 8a5c051

Browse files
committed
add notes on the callback example
1 parent e391f0b commit 8a5c051

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

assignments/callbacks.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,38 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
44

55
/*
66
7-
//Given this problem:
8-
7+
// GIVEN THIS PROBLEM:
8+
99
function firstItem(arr, cb) {
1010
// firstItem passes the first item of the given array to the callback function.
1111
}
1212
13-
// Potential Solution:
13+
// SOLUTION:
1414
15-
// Higher order function using "cb" as the call back
1615
function firstItem(arr, cb) {
1716
return cb(arr[0]);
1817
}
1918
20-
// Function invocation
21-
firstItem(items, function(first) {
22-
console.log(first)
23-
});
19+
// NOTES ON THE SOLUTION:
20+
21+
// firstItem is a higher order function
22+
// that uses a callback (referred to as 'cb') as its second argument.
23+
// To test our solution, we will try it with a variety of callbacks.
24+
// Note that callbacks might be declared separately, or inlined.
25+
26+
// TEST 1 (inlined callback):
27+
28+
firstItem(items, item => console.log(`I love my ${item}!`));
29+
// "I love my Pencil!"
30+
31+
// TEST 2 (declaring callback previously):
32+
33+
function exorbitantPrice(thing) {
34+
console.log(`this ${thing} costs a million dollars!`);
35+
};
2436
37+
firstItem(items, exorbitantPrice);
38+
// "this Pencil costs a million dollars!"
2539
*/
2640

2741

0 commit comments

Comments
 (0)