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
50 changes: 44 additions & 6 deletions assignments/array-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,69 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c
{"id":49,"first_name":"Bel","last_name":"Alway","email":"[email protected]","shirt_size":"S","company_name":"Voolia","donation":107},
{"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","shirt_size":"M","company_name":"Gabtype","donation":171}];

// ==== Challenge 1: Use .forEach() ====
console.log('\n==== 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 into a new array called fullName.
let fullName = [];
runners.forEach(runner => {
fullName.push(`${runner["first_name"]} ${runner["last_name"]}`)
})
console.log(fullName);

// ==== Challenge 2: Use .map() ====


console.log('\n==== Challenge 2: Use .map() ====');
// The event director needs to have all the runner's first names converted to uppercase because the director BECAME DRUNK WITH POWER. Convert each first name into all caps and log the result
let allCaps = [];
allCaps = runners.map(runner => {
return runner["first_name"].toUpperCase();
})
console.log(allCaps);

// ==== Challenge 3: Use .filter() ====


console.log('\n==== Challenge 3: Use .filter() ====');
// The large shirts won't be available for the event due to an ordering issue. Get a list of runners with large sized shirts so they can choose a different size. Return an array named largeShirts that contains information about the runners that have a shirt size of L and log the result
let largeShirts = [];
largeShirts = runners.filter(runner => runner["shirt_size"] === "L")
console.log(largeShirts);

// ==== Challenge 4: Use .reduce() ====


console.log('\n==== Challenge 4: Use .reduce() ====');
// The donations need to be tallied up and reported for tax purposes. Add up all the donations into a ticketPriceTotal array and log the result
let ticketPriceTotal = [];
ticketPriceTotal.push(runners.reduce((total, runner) => {
return total + runner["donation"];
}, 0))
console.log(ticketPriceTotal);

// ==== Challenge 5: Be Creative ====
console.log('\n==== 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
// The event director wants to see how many companies where represented at the event in alphabetical order.
let companies = [];
runners.forEach(runner => {
if(!companies.includes(runner["company_name"])) {
companies.push(runner["company_name"])
}
})
console.log(companies.sort())

// Problem 2
// The event director wants to see how much each company gave in donations. Use the solution from Problem 1 and create a new array with objects containing company name and amont given.
let companyAmounts = [];
companies.forEach(company => {
const amount = runners.reduce((total, runner) => {
return runner["company_name"] === company ? total + runner["donation"] : total;
}, 0);
companyAmounts.push({company, amount})
})
console.log(companyAmounts)

// Problem 3
// The event director wants to send personalized emails to each company thanking them for their specific donation amounts. Use solution from Problem 2.
let thankYouLetters = [];
thankYouLetters = companyAmounts.map(companyamount => `On behalf of our community: Thank you ${companyamount.company} for your gift of $${companyamount.amount}.`);

// Problem 3
console.log(thankYouLetters)
41 changes: 38 additions & 3 deletions assignments/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,68 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum'];
});

*/
function consoleLoggerHandler(result) {
console.log(result);
}


function getLength(arr, cb) {
// getLength passes the length of the array into the callback.
cb(`Length of array is: ${arr.length}`);
}
getLength(items, consoleLoggerHandler)



function last(arr, cb) {
// last passes the last item of the array into the callback.
cb(`Last item in array is: ${arr[arr.length - 1]}`);
}
last(items, consoleLoggerHandler);



function sumNums(x, y, cb) {
// sumNums adds two numbers (x, y) and passes the result to the callback.
const sum = x + y;
cb(`The sum is: ${sum}`);
}
sumNums(10,4, consoleLoggerHandler)



function multiplyNums(x, y, cb) {
// multiplyNums multiplies two numbers and passes the result to the callback.
const product = x * y;
cb(`The product is: ${product}`);
}
multiplyNums(5, 5, consoleLoggerHandler)



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.
let found = false;
for (let index = 0; index < list.length; index++) {
if(list[index] === item) {
found = true;
}
}
cb(`It is ${found} that ${item} is in our list`);
}
contains('Notebook', items, consoleLoggerHandler);




/* 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.
let noDuplicates = [];
array.forEach(item => {
if (!noDuplicates.includes(item)) {
noDuplicates.push(item);
}
})
}
52 changes: 43 additions & 9 deletions assignments/closure.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const simpleClosure = () => {
debugger;
const closures = "closures";
const closuresAreCool = () => {
debugger;
console.log(`${closures} are cool!`)
}
closuresAreCool();
}
simpleClosure();



// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
};
const counter = (count = 0) => increment = () => {
count++;
return count;
}
const newCounter = counter();
console.log(newCounter());
console.log(newCounter());

// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2

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



/* STRETCH PROBLEM, Do not attempt until you have completed all previous tasks for today's project files */
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
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.
};

// 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.
const counterFactory = () => objectFunction = {
count: 0,
increment: () => {
objectFunction.count++
return objectFunction.count;
},
decrement: () => {
objectFunction.count--
return objectFunction.count;
}
}

const counter2 = counterFactory();

console.log(counter2.increment());
console.log(counter2.decrement());

9 changes: 8 additions & 1 deletion assignments/stretch-function-conversion.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
// Take the commented ES5 syntax and convert it to ES6 arrow Syntax

// let myFunction = function () {};
const myFunction = () => {};

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

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

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

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

const triple = exampleArray.map(num => num * 3);

// console.log(triple);