diff --git a/assignments/array-methods.js b/assignments/array-methods.js index f692c35c6..d5072e65e 100644 --- a/assignments/array-methods.js +++ b/assignments/array-methods.js @@ -1,8 +1,11 @@ -// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small donation on their behalf for some much needed updates to their facilities. Each business has assigned a representative to attend the event along with a small donation. +// A local community center is holding a fund raising 5k fun run and has invited 50 small businesses to make a small +// donation on their behalf for some much needed updates to their facilities. +// Each business has assigned a representative to attend the event along with a small donation. // Scroll to the bottom of the list to use some advanced array methods to help the event director gather some information from the businesses. -const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"cseiler0@wired.com","shirt_size":"2XL","company_name":"Divanoodle","donation":75}, +const runners = +[{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"cseiler0@wired.com","shirt_size":"2XL","company_name":"Divanoodle","donation":75}, {"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"wierland1@angelfire.com","shirt_size":"2XL","company_name":"Wordtune","donation":148}, {"id":3,"first_name":"Julieta","last_name":"McCloid","email":"jmccloid2@yahoo.com","shirt_size":"S","company_name":"Riffpedia","donation":171}, {"id":4,"first_name":"Martynne","last_name":"Paye","email":"mpaye3@sciencedaily.com","shirt_size":"XL","company_name":"Wordware","donation":288}, @@ -54,30 +57,48 @@ 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) => 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 = runners.filter((runner)=> 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 = runners.reduce((acc, currentValue) => { + return acc = acc + 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 +// Organize the data into array that shows the company alongside the donation amount dorted by highest donation. +let topGivers = runners.map((runner) => { + return {"company": runner.company_name, "donation": runner.donation}; +}) +let sortedTopGivers = topGivers.sort((a,b) => b.donation - a.donation) +console.log(sortedTopGivers); // Problem 2 - -// Problem 3 \ No newline at end of file +// Make an easy to read contact list of Just the First and Last name plus Email +let contactList = runners.map((runner)=> { + return `${runner.first_name} ${runner.last_name} - E-mail: ${runner.email}` +}); +console.log(contactList); +// Problem 3 +// List all the runners and sort alphabetically +let nameList = runners.map((runner)=> { + return `${runner.first_name} ${runner.last_name}` +}); +let sortedNameList = nameList.sort(); +console.log(sortedNameList); \ No newline at end of file diff --git a/assignments/callbacks.js b/assignments/callbacks.js index c1c013800..218de1835 100644 --- a/assignments/callbacks.js +++ b/assignments/callbacks.js @@ -1,11 +1,13 @@ -// 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. +// 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: - //Given this problem: - function firstItem(arr, cb) { // firstItem passes the first item of the given array to the callback function. } @@ -17,39 +19,53 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; return cb(arr[0]); } - // Function invocation + // Function invocation firstItem(items, function(first) { console.log(first) }); */ - +const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; function getLength(arr, cb) { - // getLength passes the length of the array into the callback. -} +// getLength passes the length of the array into the callback. + cb("Array Length is " + arr.length); +}; +getLength(items, console.log); + + function last(arr, cb) { // last passes the last item of the array into the callback. + cb("Last item is " + arr[arr.length-1]); } +last(items, console.log); function sumNums(x, y, cb) { // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x+y); } +sumNums(5,5, console.log); function multiplyNums(x, y, cb) { // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x*y); } +multiplyNums(5,2, console.log); +// 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) { - // 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('Gum', items, console.log); /* STRETCH PROBLEM */ +// removeDuplicates removes all duplicate values from the given array. +// Pass the duplicate free array to the callback function. +// Do not mutate the original array. 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 newArray = array.filter( item => item); + cb(newArray) } diff --git a/assignments/closure.js b/assignments/closure.js index 4307524fc..812d34a2c 100644 --- a/assignments/closure.js +++ b/assignments/closure.js @@ -1,6 +1,12 @@ // ==== Challenge 1: Write your own closure ==== // Write a simple closure of your own creation. Keep it simple! - +function greetWorld(greeting){ + let hello = greeting; + (function(){ + console.log(`${hello} World!`); + })(); +} +greetWorld("greetings"); /* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */