-
Notifications
You must be signed in to change notification settings - Fork 436
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
tweak the reduce exercise for beginner friendliness #168
base: master
Are you sure you want to change the base?
Conversation
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.
Some minor changes needed but this looks good! Thanks.
Wish we had these explanations for filter & map too.
countMap[word] = ++countMap[word] || 1 // increment or initialize to 1 | ||
return countMap | ||
}, {}) // second argument to reduce initialises countMap to {} | ||
return arr.reduce(function(accumulator, current_word) { |
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.
The original argument names were intended to be "semantic", related to the problem, rather than generic "accumulator". i.e. you're creating a map
of counts
for words
.
Reduce functions can be hard to follow, so the idea is to embed as much semantic information into the argument names as possible. I'm open to better names, so long as they're not generic.
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.
I see your point. Thinking of it as an accumulator helped me understand reduce better, but using semantic naming is also helpful.
This single value is often called the accumulator because it is inside of this variable that values are accumulated. | ||
|
||
When you use the javascript array method for reduce, many of the examples have a one return for this method, but it can be easier | ||
to return the accumulator after performing your work. |
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.
I'd just drop this paragraph. Better to just present one style and not mention others.
accumulator = accumulator + current_value | ||
|
||
return accumulator; | ||
}, 0); |
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.
For consistency with other code samples, please drop unnecessary semicolons, unnecessary line breaks and use camelCase for variables. :D
…es back to original
In working through the reduce example at the Oakland Node school today, my pair and I thought that this tweak to the suggested solution along with a helpful hint would make the reduce example a little bit easier to solve.
Let me know if there are any additional tweaks or changes I should make.
Thanks for making this workshopper. It has been really fun!