Skip to content

Commit a0285bd

Browse files
committed
solves callbacks.js problems (except for stretch problems)
1 parent bc4a039 commit a0285bd

1 file changed

Lines changed: 33 additions & 2 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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.
23

34
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
45

@@ -27,25 +28,55 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
2728

2829
function getLength(arr, cb) {
2930
// getLength passes the length of the array into the callback.
31+
return cb(arr.length);
3032
}
3133

34+
getLength(items, (length) => {
35+
console.log(length);
36+
})
37+
3238
function last(arr, cb) {
3339
// 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+
})
3546

3647
function sumNums(x, y, cb) {
3748
// sumNums adds two numbers (x, y) and passes the result to the callback.
49+
return cb(x+y);
3850
}
3951

52+
sumNums(1, 2, (sum) =>{
53+
console.log(sum);
54+
});
55+
4056
function multiplyNums(x, y, cb) {
4157
// multiplyNums multiplies two numbers and passes the result to the callback.
58+
return cb(x*y);
4259
}
4360

61+
multiplyNums(2, 2, (product) =>{
62+
console.log(product);
63+
});
64+
4465
function contains(item, list, cb) {
4566
// contains checks if an item is present inside of the given array/list.
4667
// Pass true to the callback if it is, otherwise pass false.
68+
return cb(list.includes(item));
4769
}
4870

71+
contains('Pencil', items, (boolValue) => {
72+
console.log(boolValue);
73+
})
74+
75+
contains('Pickle', items, (boolValue) => {
76+
console.log(boolValue);
77+
})
78+
79+
4980
/* STRETCH PROBLEM */
5081

5182
function removeDuplicates(array, cb) {

0 commit comments

Comments
 (0)