Addison Stavlo - Project Complete - #430
Conversation
| // ==== 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( item => fullName.push(item.first_name + ' ' + item.last_name)); |
There was a problem hiding this comment.
Good use of arrow functions to keep the code terse.
| // ==== 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((total,item)=> total + item.donation, 0); |
There was a problem hiding this comment.
Can you guess what happens if you did not provide the second argument to reduce? What is the output? Does it throw an error?
|
|
||
| // Problem 1 | ||
| // change everyones first name to their last name!? | ||
| runners.forEach( item => item.first_name = item.last_name); |
There was a problem hiding this comment.
Don't you think map is better suited for this job to keep the original array intact and thereby not affecting the code that follows this? How would you achieve the same task with map?
| // Problem 3 No newline at end of file | ||
| // Problem 3 | ||
| // new array with list of people of shirt size L who donated less than $10, or size XL who donated less than $20 | ||
| let largeShirtsSmallDonations = runners.filter(item => (item.shirt_size === "L" && item.donation < 10) || (item.shirt_size === "XL" && item.donation < 20)); |
There was a problem hiding this comment.
This is the most useful use of filter for the company that gives the shirts.
| // Do not mutate the original array. | ||
| let newArray = []; | ||
| for (i=0;i<array.length;i++){ | ||
| if(newArray.indexOf(array[i]) === -1 ){ |
There was a problem hiding this comment.
Array#includes might help you shorten your code here.
| // 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.map(item => item); |
There was a problem hiding this comment.
Array#slice will help you produce a shallow copy of the array. This code is perfectly valid too.
| const counter = () => { | ||
| // Return a function that when invoked increments and returns a counter variable. | ||
| let count = 0; | ||
| return () => ++count; |
There was a problem hiding this comment.
That's a really cool one-liner function.
@ashwins93