diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..0ff7ed0b6 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,48 +1,72 @@ // Create a callback function and invoke the 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. -const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; +const items = ["Pencil", "Notebook", "yo-yo", "Gum"]; -/* +// //Given this problem: - //Given this problem: - - function firstItem(arr, cb) { - // firstItem passes the first item of the given array to the callback function. - } +// function firstItem(arr, cb) { +// // firstItem passes the first item of the given array to the callback function. +// } - // Potential Solution: - function firstItem(arr, cb) { - return cb(arr[0]); - } +// Potential Solution: +// function firstItem(arr, cb) { +// return cb(arr[0]); +// } - firstItem(items, function(first) { - console.log(first) - }); +// firstItem(items, function(first) { +// console.log(first); +// }); -*/ +// function getLength(arr, cb) { +// // getLength passes the length of the array into the callback. +// return cb(arr.length); +// } +// getLength(items, function(length) { +// console.log(length); +// }); -function getLength(arr, cb) { - // getLength passes the length of the array into the callback. -} +// function last(arr, cb) { +// // last passes the last item of the array into the callback. +// return cb(arr[arr.length - 1]); +// } -function last(arr, cb) { - // last passes the last item of the array into the callback. -} +// last(items, function(lastItem) { +// console.log(lastItem); +// }); -function sumNums(x, y, cb) { - // sumNums adds two numbers (x, y) and passes the result to the callback. -} +// function sumNums(x, y, cb) { +// // sumNums adds two numbers (x, y) and passes the result to the callback. +// return cb(x + y); +// } -function multiplyNums(x, y, cb) { - // multiplyNums multiplies two numbers and passes the result to the callback. -} +// sumNums(2, 3, function(sum) { +// console.log(sum); +// }); + +// function multiplyNums(x, y, cb) { +// // multiplyNums multiplies two numbers and passes the result to the callback. +// return cb(x * y); +// } + +// multiplyNums(3, 2, function(multiply) { +// console.log(multiply); +// }); function contains(item, list, cb) { // contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. + if (list.indexOf(item) != -1) { + return cb(true); + } else { + return cb(false); + } } +contains("Pencil", items, function(bool) { + console.log(bool); +}); + /* STRETCH PROBLEM */ function removeDuplicates(array, cb) {