Skip to content
Open
Show file tree
Hide file tree
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
59 changes: 59 additions & 0 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,88 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];

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

let findFirst = function(arrFirstItem) {
return `The first item in this array is ${arrFirstItem}!`;
}

firstItem(items, findFirst); //?

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

let arrayLength = function(arrLen) {
return `This array's length is ${arrLen}!`;
}

getLength(items, arrayLength); //?

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

let findLast = function(arrLastItem) {
return `The last item in this array is ${arrLastItem}!`;
}

last(items, findLast); //?

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

let summed = function(x, y) {
return `${x} plus ${y} equals ${x+y}.`;
}

sumNums(1,2,summed); //?

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

let produced = function(x, y) {
return `${x} times ${y} equals ${x*y}.`;
}

multiplyNums(3,5,produced); //?

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.
for (let i = 0; i < list.length; i++) {
x = item;
trueOrFalse = null;
if (list[i] === item) {
trueOrFalse = true;
}
if (trueOrFalse === true) {
return cb(trueOrFalse, x);
}
}
trueOrFalse = false;
return cb(trueOrFalse, x);
}

let itemExists = function(trueOrFalse, x) {
if (trueOrFalse === true) {
return `${x} is a part of this array.`;
}
return `${x} was not found.`;
}

contains(items[2], items, itemExists);




/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
Expand Down
34 changes: 29 additions & 5 deletions assignments/function-conversion.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

// let myFunction = function () {};
let myFunction = () => "no params here";

myFunction(); //?

// let anotherFunction = function (param) {
// return param;
// };
let anotherFunction = (param) => param;

const theTruth = true;
anotherFunction(theTruth); //?

// let add = function (param1, param2) {
// return param1 + param2;
// };
// add(1,2);
let add = (x, y) => x + y;

add(51, 52); //?

// let subtract = function (param1, param2) {
// return param1 - param2;
// };
// subtract(1,2); //?
let subtract = (x, y) => x - y;

subtract(51, 52); //?

let subtract = function (param1, param2) {
return param1 - param2;
};
subtract(1,2); //?

exampleArray = [1,2,3,4];
// const triple = exampleArray.map(function (num) {
// return num * 3;
// });
// console.log(triple);
// console.log(triple);

const triple = function(arr) {return arr.map((num) => num * 3)}; //?
console.log(triple(exampleArray));
triple(exampleArray); //?

// split this up from only using the exampleArray array ONLY
// that way, it's more usable for other arrays; otherwise, would've written it as:

// const triple2 = exampleArray.map((num) => num * 3); //?
// console.log(triple2);