Skip to content

Commit d6e4a37

Browse files
committed
working on callbacks
1 parent bc4a039 commit d6e4a37

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

assignments/callbacks.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

5-
/*
5+
66

77
//Given this problem:
88

@@ -13,6 +13,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
1313
// Potential Solution:
1414

1515
// Higher order function using "cb" as the call back
16+
1617
function firstItem(arr, cb) {
1718
return cb(arr[0]);
1819
}
@@ -22,16 +23,39 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2223
console.log(first)
2324
});
2425

25-
*/
26+
//
2627

28+
function getLength(arr, cb) {
29+
// getLength passes the length of the array into the callback.
30+
}
2731

28-
function getLength(arr, cb) {
29-
// getLength passes the length of the array into the callback.
30-
}
32+
function getLength(arr, cb) {
33+
return cb(arr.length);
34+
}
3135

32-
function last(arr, cb) {
33-
// last passes the last item of the array into the callback.
34-
}
36+
getLength(items, function(length) {
37+
console.log(length)
38+
});
39+
40+
41+
42+
//
43+
44+
function last(arr, cb) {
45+
// last passes the last item of the array into the callback.
46+
}
47+
48+
function last(arr, cb) {
49+
return cb(arr[arr.length-1]);
50+
}
51+
52+
53+
last(items, function(anything){
54+
console.log(anything)
55+
});
56+
57+
58+
//
3559

3660
function sumNums(x, y, cb) {
3761
// sumNums adds two numbers (x, y) and passes the result to the callback.

0 commit comments

Comments
 (0)