|
1 | 1 | const getFirstItem = (collection, cb) => { |
2 | 2 | // invoke the callback function and pass the first item from the collection in as an argument |
| 3 | + cb(collection[0]); |
3 | 4 | }; |
4 | 5 |
|
5 | 6 | const getLength = (collection, cb) => { |
6 | 7 | // Write a function called getLength that passes the length of the array into the callback |
| 8 | + cb(collection.length); |
7 | 9 | }; |
8 | 10 |
|
9 | 11 | const getLastItem = (collection, cb) => { |
10 | 12 | // Write a function called getLastItem which passes the getLastItem item of the array into the callback |
| 13 | + cb(collection[collection.length - 1]); |
11 | 14 | }; |
12 | 15 |
|
13 | 16 | const sumNums = (x, y, cb) => { |
14 | 17 | // Write a function called sumNums that adds two numbers and passes the result to the callback |
| 18 | + cb(x + y); |
15 | 19 | }; |
16 | 20 |
|
17 | 21 | const multiplyNums = (x, y, cb) => { |
18 | 22 | // Write a function called multiplyNums that multiplies two numbers and passes the result to the callback |
| 23 | + cb(x * y); |
19 | 24 | }; |
20 | 25 |
|
21 | 26 | const contains = (collection, item, cb) => { |
22 | 27 | // Write a function called contains that checks if an item is present inside of the given array. |
23 | 28 | // Pass true to the callback if it is, otherwise pass false |
| 29 | + cb(collection.includes(item)); |
24 | 30 | }; |
25 | 31 |
|
26 | 32 | const removeDuplicates = (collection, cb) => { |
27 | 33 | // Write a function called removeDuplicates that removes all duplicate values from the given array. |
28 | 34 | // Pass the array to the callback function. Do not mutate the original array. |
| 35 | + const arr = []; |
| 36 | + for (let i = 0; i < collection.length; i++) { |
| 37 | + if (!arr.includes(collection[i])) { |
| 38 | + arr.push(collection[i]); |
| 39 | + } |
| 40 | + } |
| 41 | + cb(arr); |
29 | 42 | }; |
30 | 43 |
|
31 | 44 | module.exports = { |
|
0 commit comments