Skip to content

Commit 23eafbe

Browse files
committed
Callbacks
1 parent f659bf5 commit 23eafbe

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

‎assignments/callbacks.js‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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.
2+
// You have been provided an example of a problem and a solution to see how this works with our items array.
3+
// Study both the problem and the solution to figure out the rest of the problems.
24

3-
const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
5+
const items = ["Pencil", "Notebook", "yo-yo", "Gum"];
46

57
/*
68
@@ -38,26 +40,33 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
3840
console.log(test2); // "this Pencil is worth a million dollars!"
3941
*/
4042

41-
4243
function getLength(arr, cb) {
4344
// getLength passes the length of the array into the callback.
45+
return cb(arr.length);
4446
}
4547

4648
function last(arr, cb) {
4749
// last passes the last item of the array into the callback.
50+
return cb(arr[arr.length - 1]);
4851
}
4952

5053
function sumNums(x, y, cb) {
5154
// sumNums adds two numbers (x, y) and passes the result to the callback.
55+
let sum = x + y;
56+
return cb(sum);
5257
}
5358

5459
function multiplyNums(x, y, cb) {
5560
// multiplyNums multiplies two numbers and passes the result to the callback.
61+
let multiply = x * y;
62+
return cb(multiply);
5663
}
5764

5865
function contains(item, list, cb) {
5966
// contains checks if an item is present inside of the given array/list.
6067
// Pass true to the callback if it is, otherwise pass false.
68+
let result = list.includes(item);
69+
return cb(result);
6170
}
6271

6372
/* STRETCH PROBLEM */
@@ -66,4 +75,11 @@ function removeDuplicates(array, cb) {
6675
// removeDuplicates removes all duplicate values from the given array.
6776
// Pass the duplicate free array to the callback function.
6877
// Do not mutate the original array.
78+
const duplicateFree = [];
79+
array.map(item => {
80+
if (!duplicateFree.includes(item)) {
81+
duplicateFree.push(item);
82+
}
83+
});
84+
return cb(duplicateFree);
6985
}

0 commit comments

Comments
 (0)