Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
//Given this problem:

function firstItem(arr, cb) {
// firstItem passes the first item of the given array to the callback function.
cb(arr)
}

// Potential Solution:
Expand All @@ -24,28 +24,62 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

*/


//Challenge 1
function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr.length);
}

getLength(items, function(arrLength) {
console.log(arrLength);
})

// Challenge 2
function last(arr, cb) {
// last passes the last item of the array into the callback.
return cb(arr[arr.length -1]);
}

last(items, function(lastItem) {
console.log(lastItem);
})

// Challenge 3
function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
return cb(x + y);
}

const summed = function(addNums) {
console.log(addNums);
}

sumNums(3, 4, summed);

// Challenge 4
function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x * y);
}

multiplyNums(3, 4, function(mult) {
console.log(mult)
})

// Challenge 5
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.includes(item)) {
return cb(true)
} else {
return cb(false);
}
}

contains("Pencil", items, function(boolean) {
console.log(boolean);
})

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down