-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Initial PR #179
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?
Initial PR #179
Changes from all commits
c35e24c
65b2a91
b59faac
9c3fe58
38695cc
bcd6baa
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 |
|---|---|---|
|
|
@@ -55,29 +55,86 @@ const runners = [{"id":1,"first_name":"Charmain","last_name":"Seiler","email":"c | |
|
|
||
| // ==== 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 = []; | ||
| let fullName = runners.forEach(function(element, index) { | ||
| element["first_name"] + " " + element["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 = []; | ||
| let allCaps = runners.map(function(x) { | ||
| x.first_name.toUpperCase(); | ||
|
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. As is, this exercise is not logging out to the console. Why do you think that is? |
||
| }); | ||
| 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(function(x) { | ||
| if(x.shirt_size === "L"; | ||
|
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. As is, this exercise is not logging out to the console. Why do you think that is? |
||
| }); | ||
| 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((reducer, runner) => { | ||
| return reducer += runner.donation; | ||
|
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. No need for |
||
| }, 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 solve 3 unique problems using one or many of the array methods listed above. | ||
|
|
||
| // Problem 1 | ||
| // We need an email list of all Runners to send out annual reminders for our marathon event. Collect all runner's emails. | ||
| let emailList = []; | ||
|
|
||
| runners.forEach(function(participant) { | ||
| emailList.push(participant["donation"]); | ||
| }); | ||
|
|
||
| console.log(emailList); | ||
|
|
||
| // Problem 2 | ||
| // This year, we are splitting the runners into two teams to see which teams time is the fastest. Place even id numbered runners in teamBlue and odd id numbered runners into teamRed. | ||
|
|
||
| let teamBlue = []; | ||
| let teamRed = []; | ||
|
|
||
| runners.filter(function(participant) { | ||
| if(participant.id % 2 === 0) { | ||
| teamBlue.push(participant); | ||
| } else { | ||
| teamRed.push(participant); | ||
| } | ||
| }) | ||
|
|
||
| console.log(teamBlue); | ||
| console.log(teamRed); | ||
|
|
||
| // Problem 3 | ||
| // This year we are making acknowledgements to businesses who donated an oustanding amount to the marathon. If donators donated more than 200, collect their information into the mvpDonator array. | ||
|
|
||
| let superNova= []; | ||
| let theFlash = []; | ||
| let babyCheetah = []; | ||
|
|
||
| runners.filter(function(participant) { | ||
| if(participant.donation >= 200) { | ||
| superNova.push(participant); | ||
| } else if(participant.donation >= 100) { | ||
| theFlash.push(participant); | ||
| } else { | ||
| babyCheetah.push(participant) | ||
| } | ||
| }) | ||
|
|
||
| // Problem 3 | ||
| console.log(superNova); | ||
| console.log(theFlash); | ||
| console.log(babyCheetah); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,88 @@ | ||
| const items = ['Pencil', 'Notebook', 'yo-yo', 'Gum']; | ||
|
|
||
| // Made PR | ||
|
|
||
| function firstItem(arr, cb) { | ||
| // firstItem passes the first item of the given array to the callback function. | ||
| return cb(arr); | ||
| } | ||
|
|
||
| function firstIndex(items) { | ||
| return (items[0]); | ||
| } | ||
|
|
||
| firstItem(items, firstIndex); | ||
|
|
||
| // | ||
|
|
||
| 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(arr); | ||
|
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. If you don't return this value, it will log out as undefined. |
||
| } | ||
|
|
||
| function arrLength(items) { | ||
| return items.length; | ||
| } | ||
|
|
||
| getLength(items, arrLength); | ||
|
|
||
| // | ||
|
|
||
| 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. | ||
| cb(lastIndex); | ||
|
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.
|
||
| } | ||
|
|
||
| function lastIndex(items) { | ||
| return items[items.length - 1]; | ||
| } | ||
|
|
||
| last(items, lastIndex); | ||
|
|
||
| // | ||
|
|
||
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| cb(x, y); | ||
|
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. There's something you need to do here to get the sum to print or return to the console when you run the file. What needs to happen? |
||
| } | ||
|
|
||
| function sum (a, b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| sumNums(2, 4, sum); | ||
|
|
||
| // | ||
|
|
||
| function multiplyNums(x, y, cb) { | ||
| // multiplyNums multiplies two numbers and passes the result to the callback. | ||
| cb(x, y); | ||
| } | ||
|
|
||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| multiplyNums(5, 5, 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(item, list); | ||
| } | ||
|
|
||
| function listChecker(item, list) { | ||
| for(let i = 0; i < list.length; i++) { | ||
| if(item === list[i]) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| contains('Pencil', items, listChecker); | ||
|
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. You're currently checking if an item exists in the array and returning true if it does, but you're not returning false if the item does not exist. |
||
|
|
||
| /* 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.
forEachreturns undefined, which is why when you console.log fullName, it comes returns an array of undefined items. How would you mitigate this?