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
45 changes: 44 additions & 1 deletion assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,71 @@ 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(function(items){
return fullNames.push(`${items.first_name} ${items.last_name}`);

});
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(function(items){
return firstNamesAllCaps.push(`${items.last_name.toUpperCase()} BECAME DRUNK WITH POWER`);

});
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 = [];
runnersLargeSizeShirt = runners.filter(function(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;
ticketPriceTotal = runners.reduce(function(accumulator, currentValue){
return accumulator + 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
// an email list of all the particpants is needed for email blasts regarding future charity runs, provide a list of all first and last names and their emails//
let runnersEmailList = [];

runners.forEach(function(items){
return runnersEmailList.push(`${items.first_name} ${items.last_name} ${items.email}`);
});
console.log(runnersEmailList);

// Problem 2
// We want to send a special thank you out to all donations over $100, we need a list of the first/last names of everyone who donated over $100//

let thankYou = [];

runners.forEach(function(items){
if (items.donation > 100){
thankYou.push(`${items.first_name} ${items.last_name} THANK YOU FOR YOUR GENEROUS DONATION!`);
}
});
console.log(thankYou);



// Problem 3
// Make a list of all companies with employees that took part in the run and sort them alphabetically//
let companyList = [];

// Problem 3
runners.forEach(function(items){
return companyList.push(`${items.company_name}`);
});
companyList.sort();
console.log(companyList);
34 changes: 27 additions & 7 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,47 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
console.log(test2); // "this Pencil is worth a million dollars!"
*/


// getLength passes the length of the array into the callback.
function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
return cb(arr.length)
}
const test1 = getLength(items, item => `the length of this array is ${item}!`);
console.log(test1);

// last passes the last item 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]);
}
const test2 = last(items, item2 => `the last item is ${item2}!`);
console.log(test2);

// 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);
}
const add = (x, y) =>{
return x + y;
}
console.log(sumNums(3, 3, add));

// multiplyNums multiplies two numbers and passes the result to the callback.
function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
return cb(x, y);
}
const multiply = (x, y) =>{
return x * y;
}
console.log(multiplyNums(4, 5, multiply));

function contains(item, list, cb) {
// contains checks if an item is present inside of the given array/list.

// contains checks if an item is present inside of the given array/list.
// Pass true to the callback if it is, otherwise pass false.
function contains(item, list, cb) {
return cb(item, list);
}
const included = (item, list) => list.includes(item);
console.log(contains('Notebook', items, included));
console.log(contains('poop', items, included));

/* STRETCH PROBLEM */

Expand Down
19 changes: 15 additions & 4 deletions assignments/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,30 @@
// that manipulates variables defined in the outer scope.
// The outer scope can be a parent function, or the top level of the script.

function Challenge1(computer){
const feat1 = 'Windows 10'
const feat2 = 'Skype'
function processor(cpu){
console.log(`${computer} is the best computer on the market, it comes with ${feat1}, ${feat2} and an ${cpu} CPU.`);
}
processor('Intel Core i7-8565U');

};
Challenge1('Dell XPS 13');


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


// ==== Challenge 2: Implement a "counter maker" function ====
const counterMaker = () => {
// const counterMaker = () => {
// IMPLEMENTATION OF counterMaker:
// 1- Declare a `count` variable with a value of 0. We will be mutating it, so declare it using `let`!
// 2- Declare a function `counter`. It should increment and return `count`.
// NOTE: This `counter` function, being nested inside `counterMaker`,
// "closes over" the `count` variable. It can "see" it in the parent scope!
// 3- Return the `counter` function.
};
// };
// Example usage: const myCounter = counterMaker();
// myCounter(); // 1
// myCounter(); // 2
Expand All @@ -26,8 +37,8 @@ const counterMaker = () => {
// will refuse to go over the limit, and start back at 1.

// ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
const counterFactory = () => {
// const counterFactory = () => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
};
// };