diff --git a/assignments/array-methods.js b/assignments/array-methods.js index c2d782f3b..7d347dfce 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -54,30 +54,117 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c {"id":50,"first_name":"Shell","last_name":"Baine","email":"sbaine1d@intel.com","shirt_size":"M","company_name":"Gabtype","donation":171}]; // ==== 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. +// 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() ==== // 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 = []; -console.log(allCaps); +// let allCaps = []; +// runners.map((runner) =>{ +// allCaps.push(runner.first_name.toUpperCase()); +// }) +let allCaps = runners.map((runner) => { + return runner.first_name.toUpperCase(); +}); +console.log(allCaps); // ==== 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 = []; +// let largeShirts = []; + +let largeShirts = runners.filter((runner) => { + return runner.shirt_size === 'L'; +}); + console.log(largeShirts); + + // ==== 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 = []; +// let ticketPriceTotal = []; +let ticketPriceTotal = runners.reduce((acccumulator, currentValue ) => { + return acccumulator + 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 +// Company teams + +makeCompanyTeam = (company, arr) => { + let companyTeam = arr.filter((entry) => { + return entry.company_name === company; + }); + return companyTeam; +}; +console.log(makeCompanyTeam('Skinix', runners)); +console.log(makeCompanyTeam('Gigashots', runners)); + // Problem 2 +//Team fundraising + +companyTeamTotal = (company, arr, cb) => { + let companyTotal = cb(company, arr).reduce((accumulator, runner) => { + return accumulator + runner.donation; + }, 0); + return companyTotal; +}; +console.log(companyTeamTotal('Skinix', runners, makeCompanyTeam)); +console.log(companyTeamTotal('Gigashots', runners, makeCompanyTeam)) + +//Problem 3 +//Company with the highest average donation + +let companies = []; +runners.forEach((runner) => { + companies.push(`${runner.company_name}`); +}); + +let uniqueCompanies = companies.filter(function(item, pos) { + return companies.indexOf(item) == pos; + }); + + +const findHighestAverage = (search, array, teamcb, totalcb) => { + let currentHighestAverage = 0; + let currentHighestCompany = []; + search.forEach((company) => { + let currentTeamAverage = (totalcb(company, runners, teamcb)) / teamcb(company, runners).length; + if (currentTeamAverage > currentHighestAverage){ + currentHighestAverage = currentTeamAverage; + currentHighestCompany = company; + } + }); + return `${currentHighestCompany}\'s team raised an average of $${currentHighestAverage} per person!`; +}; + + console.log(findHighestAverage(uniqueCompanies, runners, makeCompanyTeam, companyTeamTotal)); + + // Problem 4 + //Special gift email list for donating over $100. + + let specialGift = runners.filter((runner) => { + return runner.donation > 100; + }); + let specialGiftEmails = []; + specialGift.forEach((runner) => { + specialGiftEmails.push(`${runner.first_name} ${runner.last_name}. Donation Amount: $${runner.donation}. Email: ${runner.email}`); + }); + console.log(specialGiftEmails); + +//Problem 5 because this is fun +//Average donation -// Problem 3 \ No newline at end of file +let totalDonations = runners.reduce((accumulator, runner ) => { + return accumulator + runner.donation; +}, 0); +let averageDonation = totalDonations/runners.length; +console.log(averageDonation); diff --git a/assignments/callbacks.js b/assignments/callbacks.js index d5028657e..09889aa58 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -2,10 +2,10 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; -/* +/* + + //Given this problem: - //Given this problem: - function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. } @@ -24,29 +24,65 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { // getLength passes the length of the array into the callback. + return cb(arr.length); } +getLength(items, function(length){ + console.log(length); +}) function last(arr, cb) { // last passes the last item of the array into the callback. + return cb(arr[arr.length-1]); } +last(items, function(last){ + console.log(last); +}) + function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + return cb((x + y)); } +sumNums(1, 2, function(add){ + console.log(add); +}) + function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + return cb((x * y)); } +multiplyNums(3,9, function(multiply){ + console.log(multiply); +}) 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. + cb(list.includes(item)); } +contains('Paper', items, function(isit){ + console.log(isit); +}); + +contains('Gum', items, function(isit){ + console.log(isit); +}); + + /* 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 uniqueArray = array.filter(function(item, pos, self) { + return self.indexOf(item) == pos; +}) + cb(uniqueArray); } + +removeDuplicates(items, function(test){ + console.log(test); +}); diff --git a/assignments/closure.js b/assignments/closure.js index b16c45ae4..1b12c8e4d 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,11 +1,37 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! +function greeting(namePara) { + const name = namePara; + const welfareCheck = 'How are you doing today'; + console.log(`Hey, ${name}!`); + + + function ask() { + console.log(`${welfareCheck}, ${name}?`); + + } // ask + + ask(); +} //greeting + +greeting('Kyran'); + // ==== Challenge 2: Create a counter function ==== const counter = () => { - // Return a function that when invoked increments and returns a counter variable. + let count = 0; + return () => { + return count += 1; }; +} + +const newCounter = counter(); + +console.log(newCounter ()); +console.log(newCounter ()); +console.log(newCounter ()); + // Example usage: const newCounter = counter(); // newCounter(); // 1 // newCounter(); // 2 @@ -13,8 +39,33 @@ const counter = () => { /* 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. +const counterFactory = () => { + let count = 0; +return { + increment: () => { + return count += 1; + }, + decrement: () => { + return count -= 1; + + } }; + } + +// Testing: +console.log(counterFactory()); + + + const isItWorking= counterFactory(); + + console.log(isItWorking.increment()); + console.log(isItWorking.decrement()); + + console.log(isItWorking.decrement()); + console.log(isItWorking.increment()); + console.log(isItWorking.increment()); + console.log(isItWorking.increment());