|
1 | | -// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. Study both the problem and the solution to figure out the rest of the problems. |
| 1 | +// Create a higher order function and invoke the callback function to test your work. You have been provided an example of a problem and a solution to see how this works with our items array. |
| 2 | +// Study both the problem and the solution to figure out the rest of the problems. |
2 | 3 |
|
3 | 4 | const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
4 | 5 |
|
@@ -27,25 +28,55 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
27 | 28 |
|
28 | 29 | function getLength(arr, cb) { |
29 | 30 | // getLength passes the length of the array into the callback. |
| 31 | + return cb(arr.length); |
30 | 32 | } |
31 | 33 |
|
| 34 | +getLength(items, (length) => { |
| 35 | + console.log(length); |
| 36 | +}) |
| 37 | + |
32 | 38 | function last(arr, cb) { |
33 | 39 | // last passes the last item of the array into the callback. |
34 | | -} |
| 40 | + return cb(arr[arr.length-1]); |
| 41 | +}; |
| 42 | + |
| 43 | +last(items, (lastElement) => { |
| 44 | + console.log(lastElement); |
| 45 | +}) |
35 | 46 |
|
36 | 47 | function sumNums(x, y, cb) { |
37 | 48 | // sumNums adds two numbers (x, y) and passes the result to the callback. |
| 49 | + return cb(x+y); |
38 | 50 | } |
39 | 51 |
|
| 52 | +sumNums(1, 2, (sum) =>{ |
| 53 | + console.log(sum); |
| 54 | +}); |
| 55 | + |
40 | 56 | function multiplyNums(x, y, cb) { |
41 | 57 | // multiplyNums multiplies two numbers and passes the result to the callback. |
| 58 | + return cb(x*y); |
42 | 59 | } |
43 | 60 |
|
| 61 | +multiplyNums(2, 2, (product) =>{ |
| 62 | + console.log(product); |
| 63 | +}); |
| 64 | + |
44 | 65 | function contains(item, list, cb) { |
45 | 66 | // contains checks if an item is present inside of the given array/list. |
46 | 67 | // Pass true to the callback if it is, otherwise pass false. |
| 68 | + return cb(list.includes(item)); |
47 | 69 | } |
48 | 70 |
|
| 71 | +contains('Pencil', items, (boolValue) => { |
| 72 | + console.log(boolValue); |
| 73 | +}) |
| 74 | + |
| 75 | +contains('Pickle', items, (boolValue) => { |
| 76 | + console.log(boolValue); |
| 77 | +}) |
| 78 | + |
| 79 | + |
49 | 80 | /* STRETCH PROBLEM */ |
50 | 81 |
|
51 | 82 | function removeDuplicates(array, cb) { |
|
0 commit comments