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
31 changes: 26 additions & 5 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,49 @@ const runners = [
// ==== Challenge 1: Use .forEach() ====
// The event director needs both the first and last names of each runner for their running bibs. Combine both the first and last names and populate a new array called `fullNames`. This array will contain just strings.
let fullNames = [];
runners.forEach(currentValue => {
fullNames.push(`${currentValue.first_name} ${currentValue.last_name}`)
});
// console.log(currentValue.state);
console.log(fullNames);

// ==== Challenge 2: Use .map() ====
// The event director needs to have all the runners' first names in uppercase because the director BECAME DRUNK WITH POWER. Populate an array called `firstNamesAllCaps`. This array will contain just strings.
let firstNamesAllCaps = [];
runners.map(currentValue => {
firstNamesAllCaps.push(currentValue.first_name.toUpperCase());
})
console.log(firstNamesAllCaps);

// ==== Challenge 3: Use .filter() ====
// The large shirts won't be available for the event due to an ordering issue. We need a filtered version of the runners array, containing only those runners with large sized shirts so they can choose a different size. This will be an array of objects.
let runnersLargeSizeShirt = [];
let runnersLargeSizeShirt = runners.filter(currentValue => {
return currentValue.shirt_size == "L";
})
console.log(runnersLargeSizeShirt);

// ==== Challenge 4: Use .reduce() ====
// The donations need to be tallied up and reported for tax purposes. Add up all the donations and save the total into a ticketPriceTotal variable.
let ticketPriceTotal = 0;
let ticketPriceTotal = runners.reduce((total, currentValue) => {
return total + currentValue.donation;
}, 0);
console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
// Now that you have used .forEach(), .map(), .filter(), and .reduce(). I want you to think of potential problems you could solve given the data set and the 5k fun run theme. Try to create and then solve 3 unique problems using one or many of the array methods listed above.

// Problem 1
// Problem 1: The manager want to email all person whose size is large to inform them that their size is not available

// Problem 2
let email_message = runnersLargeSizeShirt.forEach(currentValue => {
console.log(`Hi ${currentValue.first_name}, Please your size is not available you may need to change size `)
})

// Problem 3
// Problem 2: sort all the names fullnames
console.log(firstNamesAllCaps.sort());

// Problem 3: Create a new array for people that made donation more than 100 and aswell give the total numbers
let moreDonations = runners.filter(currentValue => {
return currentValue.donation > 100;
})
console.log(moreDonations);
console.log(moreDonations.length);
73 changes: 41 additions & 32 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,78 @@

// 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.

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

/*

// GIVEN THIS PROBLEM:

function firstItem(arr, cb) {
// GIVEN THIS PROBLEM:

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

// SOLUTION:
// SOLUTION:

function firstItem(arr, cb) {
return cb(arr[0]);
}
function firstItem(arr, cb) {
return cb(items[0]);
}

// NOTES ON THE SOLUTION:
// NOTES ON THE SOLUTION:

// firstItem is a higher order function.
// It expects a callback (referred to as `cb`) as its second argument.
// To test our solution, we can use the given `items` array and a variety of callbacks.
// Note how callbacks can be declared separately, or inlined.
// firstItem is a higher order function.
// It expects a callback (referred to as `cb`) as its second argument.
// To test our solution, we can use the given `items` array and a variety of callbacks.
// Note how callbacks can be declared separately, or inlined.

// TEST 1 (inlined callback):
// TEST 1 (inlined callback):

const test1 = firstItem(items, item => `I love my ${item}!`);
console.log(test1); // "I love my Pencil!"
const test1 = firstItem(items, item => `I love my ${item}!`);
console.log(test1); // "I love my Pencil!"

// TEST 2 (declaring callback before hand):
// TEST 2 (declaring callback before hand):

function logExorbitantPrice(article) {
function logExorbitantPrice(article) {
return `this ${article} is worth a million dollars!`;
};
};

const test2 = firstItem(items, logExorbitantPrice);
console.log(test2); // "this Pencil is worth a million dollars!"

const test2 = firstItem(items, logExorbitantPrice);
console.log(test2); // "this Pencil is worth a million dollars!"
*/


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

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

function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
// 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.
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x * y)
}


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.
// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
return cb(list.include(item));
}

/* STRETCH PROBLEM */

function removeDuplicates(array, cb) {
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
}
// removeDuplicates removes all duplicate values from the given array.
// Pass the duplicate free array to the callback function.
// Do not mutate the original array.
let newArray = []
}
25 changes: 25 additions & 0 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
// Keep it simple! Remember a closure is just a function
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.
const allTheClassics = [
{ id: 1, name: "Alchemist" },
{ id: 2, name: "Black Swan" },
{ id: 3, name: "Harry Potter" }
];

function bookSearch(searchTerm, callback) {
let splittedName = searchTerm.split(" ");

return callback(splittedName);
}

function shelfCheck(bookNameArray) {
bookNameArray.map(item =>
allTheClassics.map(book => {
if (book.name.includes(item)) {
console.log(
`We have "${book.name}" that fits your search term "${item}".`
);
}
})
);
}

bookSearch("You", shelfCheck);


/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
Expand Down