|
2 | 2 |
|
3 | 3 | const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
4 | 4 |
|
5 | | -/* |
6 | 5 |
|
7 | | - //Given this problem: |
8 | | - |
9 | | - function firstItem(arr, cb) { |
10 | | - // firstItem passes the first item of the given array to the callback function. |
11 | | - } |
12 | 6 |
|
13 | | - // Potential Solution: |
| 7 | +//Given this problem: |
14 | 8 |
|
15 | | - // Higher order function using "cb" as the call back |
16 | | - function firstItem(arr, cb) { |
17 | | - return cb(arr[0]); |
18 | | - } |
| 9 | +function firstItem(arr, cb) { |
| 10 | + // firstItem passes the first item of the given array to the callback function. |
| 11 | +} |
| 12 | + |
| 13 | +// Potential Solution: |
| 14 | + |
| 15 | +// Higher order function using "cb" as the call back |
| 16 | +function firstItem(arr, cb) { |
| 17 | + return cb(arr[0]); |
| 18 | +} |
| 19 | + |
| 20 | +// Function invocation |
| 21 | +firstItem(items, function (first) { |
| 22 | + console.log(first) |
| 23 | +}); |
19 | 24 |
|
20 | | - // Function invocation |
21 | | - firstItem(items, function(first) { |
22 | | - console.log(first) |
23 | | - }); |
24 | 25 |
|
25 | | -*/ |
26 | 26 |
|
27 | 27 |
|
28 | 28 | function getLength(arr, cb) { |
29 | 29 | // getLength passes the length of the array into the callback. |
| 30 | + return cb(arr.length); |
30 | 31 | } |
| 32 | +getLength(items, function (arrLength) { |
| 33 | + console.log(`Length of array: ${arrLength}`); |
| 34 | +}); |
31 | 35 |
|
32 | 36 | function last(arr, cb) { |
33 | 37 | // last passes the last item of the array into the callback. |
| 38 | + return cb(arr[arr.length - 1]); |
34 | 39 | } |
| 40 | +last(items, function (arrLast) { |
| 41 | + console.log(`Last item in array is: ${arrLast}`); |
| 42 | +}); |
| 43 | + |
| 44 | + |
| 45 | +let x = 1; |
| 46 | +let y = 1; |
35 | 47 |
|
36 | 48 | function sumNums(x, y, cb) { |
37 | 49 | // sumNums adds two numbers (x, y) and passes the result to the callback. |
| 50 | + let math = x + y; |
| 51 | + return cb(math); |
38 | 52 | } |
| 53 | +sumNums(x, y, function (addNums) { |
| 54 | + console.log(`${x} pluse ${y} is: ${addNums}`); |
| 55 | +}); |
39 | 56 |
|
40 | 57 | function multiplyNums(x, y, cb) { |
41 | 58 | // multiplyNums multiplies two numbers and passes the result to the callback. |
| 59 | + let multiply = x * y; |
| 60 | + return cb(multiply); |
42 | 61 | } |
| 62 | +multiplyNums(x, y, function (multiply) { |
| 63 | + console.log(`${x} times ${y} is: ${multiply}`); |
| 64 | +}); |
| 65 | + |
| 66 | +const list = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; |
43 | 67 |
|
44 | 68 | function contains(item, list, cb) { |
45 | 69 | // contains checks if an item is present inside of the given array/list. |
46 | 70 | // Pass true to the callback if it is, otherwise pass false. |
| 71 | + for (let i = 0; i < list.length; i++) { |
| 72 | + let listItem = list[i]; |
| 73 | + if (item === listItem) { |
| 74 | + return cb(true); |
| 75 | + } else { |
| 76 | + return cb(false); |
| 77 | + } |
| 78 | + } |
47 | 79 | } |
48 | 80 |
|
| 81 | + |
49 | 82 | /* STRETCH PROBLEM */ |
50 | 83 |
|
51 | 84 | function removeDuplicates(array, cb) { |
|
0 commit comments