Skip to content

Commit dfb3da7

Browse files
committed
completed callbacks.js
1 parent 5fefb6d commit dfb3da7

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

assignments/callbacks.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
22

33
function firstItem(arr, cb) {
44
// firstItem passes the first item of the given array to the callback function.
5+
return cb(arr[0]);
56
}
67

78
function getLength(arr, cb) {
89
// getLength passes the length of the array into the callback.
10+
return cb(arr.length);
911
}
1012

1113
function last(arr, cb) {
1214
// last passes the last item of the array into the callback.
15+
return cb(arr[arr.length-1]);
1316
}
1417

1518
function sumNums(x, y, cb) {
1619
// sumNums adds two numbers (x, y) and passes the result to the callback.
20+
return cb(x+y);
1721
}
1822

1923
function multiplyNums(x, y, cb) {
2024
// multiplyNums multiplies two numbers and passes the result to the callback.
25+
return cb(x*y);
2126
}
2227

2328
function contains(item, list, cb) {
2429
// contains checks if an item is present inside of the given array/list.
2530
// Pass true to the callback if it is, otherwise pass false.
31+
return cb(list.includes(item));
2632
}
2733

2834
/* STRETCH PROBLEM */
@@ -31,4 +37,11 @@ function removeDuplicates(array, cb) {
3137
// removeDuplicates removes all duplicate values from the given array.
3238
// Pass the duplicate free array to the callback function.
3339
// Do not mutate the original array.
34-
}
40+
const noDuplicates = [];
41+
for(num in array){
42+
if (noDuplicates.includes(array[num]) === false){
43+
noDuplicates.push(array[num]);
44+
}
45+
}
46+
return cb(noDuplicates);
47+
}

0 commit comments

Comments
 (0)