|
1 | 1 | const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
2 | 2 |
|
3 | 3 | function firstItem(arr, cb) { |
4 | | - // firstItem passes the first item of the given array to the callback function. |
| 4 | + return cb(arr[0]); |
5 | 5 | } |
6 | | - |
| 6 | +firstItem(items, function(first) { |
| 7 | + console.log(first); |
| 8 | +}); |
| 9 | +//------------------------------------------// |
7 | 10 | function getLength(arr, cb) { |
8 | | - // getLength passes the length of the array into the callback. |
| 11 | + return cb(arr[0]); |
9 | 12 | } |
10 | | - |
| 13 | +getLength(items, function() { |
| 14 | + console.log(items.length); |
| 15 | +}); |
| 16 | +//------------------------------------------// |
11 | 17 | function last(arr, cb) { |
12 | | - // last passes the last item of the array into the callback. |
| 18 | + return cb(arr[3]); |
13 | 19 | } |
14 | | - |
| 20 | +last (items, function(final) { |
| 21 | + console.log(final); |
| 22 | +}); |
| 23 | +//------------------------------------------// |
15 | 24 | function sumNums(x, y, cb) { |
16 | | - // sumNums adds two numbers (x, y) and passes the result to the callback. |
| 25 | + return cb(x,y); |
| 26 | +} |
| 27 | + |
| 28 | +function add (x,y){ |
| 29 | + console.log (x + y) |
17 | 30 | } |
| 31 | +sumNums(2,4, add); |
18 | 32 |
|
19 | 33 | function multiplyNums(x, y, cb) { |
20 | | - // multiplyNums multiplies two numbers and passes the result to the callback. |
| 34 | + return cb(x,y); |
21 | 35 | } |
22 | 36 |
|
23 | | -function contains(item, list, cb) { |
24 | | - // contains checks if an item is present inside of the given array/list. |
25 | | - // Pass true to the callback if it is, otherwise pass false. |
| 37 | +function multiply (x,y){ |
| 38 | + console.log (x * y); |
26 | 39 | } |
| 40 | +sumNums(2,4, multiply); |
27 | 41 |
|
| 42 | +//------------------------------------------// |
28 | 43 | /* STRETCH PROBLEM */ |
29 | 44 |
|
| 45 | +// contains checks if an item is present inside of the given array/list. |
| 46 | +// Pass true to the callback if it is, otherwise pass false. |
| 47 | +function contains(item, list, cb) { |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | + |
30 | 52 | function removeDuplicates(array, cb) { |
31 | 53 | // removeDuplicates removes all duplicate values from the given array. |
32 | 54 | // Pass the duplicate free array to the callback function. |
|
0 commit comments