Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Marlena
Copy link

@Marlena Marlena commented Nov 13, 2016

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!

Copy link
Owner

@timoxley timoxley left a 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) {
Copy link
Owner

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.

Copy link
Author

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.
Copy link
Owner

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);
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants