James Phillips - #149
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(function(element){fullName.push(element.first_name + ' ' + element.last_name)}); |
There was a problem hiding this comment.
The second version is styled more preferably, though with arrow syntax you could stick this all on one line. Also, this is a good use-case for string interpolation and template literals (backticks and dollar-sign curly braces).
|
|
||
| //or// | ||
|
|
||
| let allCaps = runners.map((person) => { |
There was a problem hiding this comment.
Nice, this second one here looks really good.
|
|
||
| // Problem 2 | ||
| let ids = []; | ||
| ids = runners.filter(function(element){ |
There was a problem hiding this comment.
I don't think this .filter() is doing what you think it should. This is looking at every element in the array and checking to see if it has an id property, and if it does, then it will return that element. Therefore, since all elements in your array have ids, this filter doesn't change anything. Maybe if you did return element.donation > 100, then you could get an array of all the runners who donated more than $100. This is actually a practical use-case; maybe you want to put those runners in a unique category to recieve a special "thank you" email, for example.
| function sumNums(x, y, cb) { | ||
| // sumNums adds two numbers (x, y) and passes the result to the callback. | ||
| let sum = x + y; | ||
| cb(sum); |
There was a problem hiding this comment.
This is good. You could also have done:
cb(x + y)
| 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. | ||
| if(list.includes(item) === true) { |
There was a problem hiding this comment.
This works too:
if(list.includes(item) === true) cb(true);
else cb(false);
When one-lined, you don't need brackets, and the else will pair with the if directly above it. I would only do this for very short statements like you have here, though, and some linters don't like you to do it in the way that I'm showing you anyway because it makes it harder to edit the code by adding in more lines. Does that make sense?
| console.log(example); | ||
|
|
||
| // ==== Challenge 2: Create a counter function ==== | ||
| const counter = () => { |
There was a problem hiding this comment.
Did you grab this from the solution or from a fellow student, or did you write it yourself? Either is fine, it's just good for me to know.
| const counterFactory = () => { | ||
| let count = 0; | ||
| return { | ||
| increment: () => (++count), |
There was a problem hiding this comment.
This is fine:
increment: () => ++count,
| let anotherFunction = param => `${param}`; | ||
|
|
||
| //correct way////////////////////////// | ||
| let anotherFunction = (param) => param; |
There was a problem hiding this comment.
Good, so there is no need for the string interpolators here, and it seems like you realized that and removed them in the last example.
Callbacks done