Skip to content

Commit 8193a44

Browse files
author
mparedes003
committed
complete callbacks.js
1 parent 1146fc5 commit 8193a44

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

assignments/callbacks.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,69 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
22

33
function firstItem(arr, cb) {
44
// firstItem passes the first item of the given array to the callback function.
5+
return cb(arr);
56
}
67

8+
function first(items) {
9+
console.log(items[0]);
10+
}
11+
12+
firstItem(items, first);
13+
714
function getLength(arr, cb) {
815
// getLength passes the length of the array into the callback.
16+
return (cb(arr));
917
}
1018

11-
function last(arr, cb) {
12-
// last passes the last item of the array into the callback.
13-
}
19+
function arrLength(items) {
20+
console.log(items.length);
21+
}
1422

15-
function sumNums(x, y, cb) {
16-
// sumNums adds two numbers (x, y) and passes the result to the callback.
17-
}
23+
getLength(items, arrLength);
24+
25+
26+
function last(arr, cb) {
27+
// last passes the last item of the array into the callback.
28+
return (cb(arr));
29+
}
30+
31+
function lastItem(items) {
32+
console.log(items.pop());
33+
34+
}
35+
36+
last(items, lastItem);
37+
38+
function sumNums(x, y, cb) {
39+
// sumNums adds two numbers (x, y) and passes the result to the callback.
40+
return cb(x, y);
41+
}
42+
43+
function add(x, y) {
44+
console.log(x + y);
45+
}
46+
47+
sumNums(6,8,add);
1848

1949
function multiplyNums(x, y, cb) {
2050
// multiplyNums multiplies two numbers and passes the result to the callback.
51+
return cb(x, y);
52+
}
53+
54+
function multiply(x,y) {
55+
console.log(x * y);
2156
}
2257

58+
multiplyNums(33,25, multiply);
59+
60+
61+
/* STRETCH PROBLEMS */
62+
2363
function contains(item, list, cb) {
2464
// contains checks if an item is present inside of the given array/list.
2565
// Pass true to the callback if it is, otherwise pass false.
2666
}
2767

28-
/* STRETCH PROBLEM */
2968

3069
function removeDuplicates(array, cb) {
3170
// removeDuplicates removes all duplicate values from the given array.

0 commit comments

Comments
 (0)