|
1 | 1 | // Create a callback function and invoke the 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. |
2 | 2 |
|
3 | | -const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
| 3 | +// const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
4 | 4 |
|
| 5 | +// // function firstItem(arr, cb) { |
| 6 | +// // // firstItem passes the first item of the given array to the callback function. |
| 7 | +// // } |
| 8 | +// // Potential Solution: |
5 | 9 | // function firstItem(arr, cb) { |
6 | | -// // firstItem passes the first item of the given array to the callback function. |
| 10 | +// cb(arr[0]); |
7 | 11 | // } |
8 | | -// Potential Solution: |
9 | | -function firstItem(arr, cb) { |
10 | | - cb(arr[0]); |
11 | | -} |
12 | 12 |
|
13 | | -firstItem(items, (first) => { |
14 | | - console.log(first); |
15 | | -}); |
| 13 | +// firstItem(items, (first) => { |
| 14 | +// console.log(first); |
| 15 | +// }); |
16 | 16 |
|
17 | | -function getLength(arr, cb) { |
18 | | -// // getLength passes the length of the array into the callback. |
19 | | - console.log(cb(arr)); |
20 | | -} |
21 | | - |
22 | | - |
23 | | -let arrayLength = function(items) { |
24 | | - return items.length; |
25 | | -} |
| 17 | +// function getLength(arr, cb) { |
| 18 | +// // // getLength passes the length of the array into the callback. |
| 19 | +// console.log(cb(arr)); |
| 20 | +// } |
26 | 21 |
|
27 | | -getLength(items, arrayLength); |
28 | 22 |
|
29 | | -function last(arr, cb) { |
30 | | - // last passes the last item of the array into the callback. |
31 | | - cb(arr[arr.length - 1]); |
32 | | -} |
| 23 | +// let arrayLength = function(items) { |
| 24 | +// return items.length; |
| 25 | +// } |
33 | 26 |
|
34 | | -last(items, (lastItem) => { |
35 | | - console.log(lastItem); |
36 | | -}); |
| 27 | +// getLength(items, arrayLength); |
37 | 28 |
|
| 29 | +// function last(arr, cb) { |
| 30 | +// // last passes the last item of the array into the callback. |
| 31 | +// cb(arr[arr.length - 1]); |
| 32 | +// } |
38 | 33 |
|
| 34 | +// last(items, (lastItem) => { |
| 35 | +// console.log(lastItem); |
| 36 | +// }); |
39 | 37 |
|
40 | | -function sumNums(x, y, cb) { |
41 | | -// // sumNums adds two numbers (x, y) and passes the result to the callback. |
42 | | - cb(x + y); |
43 | | -} |
44 | 38 |
|
45 | 39 |
|
46 | | -sumNums(10, 20, (callback) => { |
47 | | - console.log(callback); |
48 | | -}); |
| 40 | +// function sumNums(x, y, cb) { |
| 41 | +// // // sumNums adds two numbers (x, y) and passes the result to the callback. |
| 42 | +// cb(x + y); |
| 43 | +// } |
49 | 44 |
|
50 | 45 |
|
51 | | -function multiplyNums(x, y, cb) { |
52 | | - // multiplyNums multiplies two numbers and passes the result to the callback. |
53 | | - cb(x * y); |
54 | | -} |
| 46 | +// sumNums(10, 20, (callback) => { |
| 47 | +// console.log(callback); |
| 48 | +// }); |
55 | 49 |
|
56 | | -multiplyNums(10, 15, function(total) { |
57 | | - console.log(total); |
58 | | -}); |
59 | 50 |
|
| 51 | +// function multiplyNums(x, y, cb) { |
| 52 | +// // multiplyNums multiplies two numbers and passes the result to the callback. |
| 53 | +// cb(x * y); |
| 54 | +// } |
60 | 55 |
|
61 | | -function contains(item, list, cb) { |
62 | | - // contains checks if an item is present inside of the given array/list. |
63 | | - // Pass true to the callback if it is, otherwise pass false. |
64 | | - for (let i = 0; i < list.length; i++) { |
65 | | - if (list[i] === item) { |
66 | | - return cb(true); |
67 | | - } else { |
68 | | - return cb(false); |
69 | | - } |
70 | | - } |
71 | | -} |
| 56 | +// multiplyNums(10, 15, function(total) { |
| 57 | +// console.log(total); |
| 58 | +// }); |
| 59 | + |
| 60 | + |
| 61 | +// function contains(item, list, cb) { |
| 62 | +// // contains checks if an item is present inside of the given array/list. |
| 63 | +// // Pass true to the callback if it is, otherwise pass false. |
| 64 | +// for (let i = 0; i < list.length; i++) { |
| 65 | +// if (list[i] === item) { |
| 66 | +// return cb(true); |
| 67 | +// } else { |
| 68 | +// return cb(false); |
| 69 | +// } |
| 70 | +// } |
| 71 | +// } |
72 | 72 |
|
73 | 73 |
|
74 | 74 |
|
75 | | -// /* STRETCH PROBLEM */ |
| 75 | +// // /* STRETCH PROBLEM */ |
76 | 76 |
|
77 | | -// function removeDuplicates(array, cb) { |
78 | | -// // removeDuplicates removes all duplicate values from the given array. |
79 | | - // Pass the duplicate free array to the callback function. |
80 | | - // Do not mutate the original array. |
81 | | -// } |
| 77 | +// // function removeDuplicates(array, cb) { |
| 78 | +// // // removeDuplicates removes all duplicate values from the given array. |
| 79 | +// // Pass the duplicate free array to the callback function. |
| 80 | +// // Do not mutate the original array. |
| 81 | +// // } |
82 | 82 |
|
83 | 83 |
|
0 commit comments