-
Notifications
You must be signed in to change notification settings - Fork 0
Meera andersen #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,12 @@ | ||||||
| // 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. | ||||||
| // 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":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75}, | ||||||
| const runners = [ | ||||||
| {"id":1,"first_name":"Charmain","last_name":"Seiler","email":"[email protected]","shirt_size":"2XL","company_name":"Divanoodle","donation":75}, | ||||||
| {"id":2,"first_name":"Whitaker","last_name":"Ierland","email":"[email protected]","shirt_size":"2XL","company_name":"Wordtune","donation":148}, | ||||||
| {"id":3,"first_name":"Julieta","last_name":"McCloid","email":"[email protected]","shirt_size":"S","company_name":"Riffpedia","donation":171}, | ||||||
| {"id":4,"first_name":"Martynne","last_name":"Paye","email":"[email protected]","shirt_size":"XL","company_name":"Wordware","donation":288}, | ||||||
|
|
@@ -51,33 +55,77 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c | |||||
| {"id":47,"first_name":"Vida","last_name":"Tydd","email":"[email protected]","shirt_size":"S","company_name":"Quaxo","donation":55}, | ||||||
| {"id":48,"first_name":"Anderea","last_name":"MacGiolla Pheadair","email":"[email protected]","shirt_size":"2XL","company_name":"Kwimbee","donation":214}, | ||||||
| {"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}]; | ||||||
| {"id":50,"first_name":"Shell","last_name":"Baine","email":"[email protected]","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. | ||||||
| let fullName = []; | ||||||
| console.log(fullName); | ||||||
| let fullName = runners.forEach(function(currentValue){ | ||||||
| // console.log(currentValue.first_name, currentValue.last_name); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
So since forEach doesn't have a return value, we have to manually populate the fullName array |
||||||
| }); | ||||||
|
|
||||||
| // console.log(fullName); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Right now this is undefined, after you follow my suggestion it should contain the correct answer |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
| // ==== 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); | ||||||
| // 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 | ||||||
|
|
||||||
| const allCaps = runners.map(currentValue => currentValue.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 = []; | ||||||
| console.log(largeShirts); | ||||||
| // 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 = runners.filter(currentValue => currentValue.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 = []; | ||||||
| console.log(ticketPriceTotal); | ||||||
| // 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 = 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. | ||||||
| // 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 | ||||||
| //As a token of our appreciation, we want to make sure our top doners receive a special 'Thank-You' card. Sort out all of the doners who donated $150 or more. | ||||||
| const topDoners = runners.filter(currentValue => currentValue.donation > 150); | ||||||
|
|
||||||
| // console.log(topDoners); | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
| // Problem 2 | ||||||
| //Using .forEach and template literals, create a list of the participants first and last name, as well as their email. | ||||||
| let contactInfo = runners.forEach(function(currentValue){ | ||||||
| // console.log(`${currentValue.first_name} ${currentValue.last_name}, ${currentValue.email}`); | ||||||
| }); | ||||||
|
|
||||||
|
|
||||||
| // Problem 3 | ||||||
| let fidel = runners.filter(currentValue => currentValue.last_name === "Fidel"); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Fidel is someone's first name in the array |
||||||
|
|
||||||
| // Problem 3 | ||||||
| console.log(fidel); | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| // 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']; | ||
|
|
||
|
|
@@ -24,28 +26,71 @@ const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | |
|
|
||
| */ | ||
|
|
||
|
|
||
| function getLength(arr, cb) { | ||
| // getLength passes the length of the array into the callback. | ||
| const length = function(array){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So they were looking for these questions to be answered in a little bit of a different format than what you have here, but you correctly demonstrated using HOFs with callbacks |
||
| return array.length; | ||
| } | ||
| function getLength(array, cb) { | ||
| return cb(array); | ||
| } | ||
| console.log(getLength(items, length)); | ||
|
|
||
|
|
||
| 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. | ||
| const lastItem = function(array){ | ||
| return array.pop(); | ||
| } | ||
| function last(array, cb) { | ||
| return cb(array); | ||
| } | ||
| console.log(last(items, lastItem)); | ||
|
|
||
|
|
||
|
|
||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| const add = function(x, y){ | ||
| return x + y; | ||
| } | ||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| return cb(x, y); | ||
| } | ||
| console.log(sumNums(2, 8, add)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| const multiply = function(x, y){ | ||
| return x * y; | ||
| } | ||
| function multiplyNums(x, y, cb) { | ||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| return cb(x, y); | ||
| } | ||
| console.log(multiplyNums(3, 3, multiply)); | ||
|
|
||
|
|
||
| function contains(item, list, cb) { | ||
| // contains checks if an item is present inside of the given array/list. | ||
| //const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | ||
| // contains checks if an item is present inside of the given array/list. | ||
| // Pass true to the callback if it is, otherwise pass false. | ||
| const includesItem = function(item, list){ | ||
| if (list.includes(item)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| function contains(item, list, cb) { | ||
| return cb(item, list); | ||
| } | ||
|
|
||
| console.log(contains("Pencil", items, includesItem)); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| /* STRETCH PROBLEM */ | ||
|
|
||
| function removeDuplicates(array, cb) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forEach has no return value, unlike map.