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

reduceRight wrong example? #78

Open
alizhdanov opened this issue Sep 3, 2018 · 3 comments
Open

reduceRight wrong example? #78

alizhdanov opened this issue Sep 3, 2018 · 3 comments

Comments

@alizhdanov
Copy link

Hello,
I'm not sure, but in find items -> one item:

reduce returns sum of the array, as I'd expect, while reduceRight 'flatten' arrays. Shouldn't it be also sum?

@QWp6t
Copy link

QWp6t commented Nov 12, 2018

They're using different demo functions, and their return values are consistent with those functions. Returning the sum is just a common example use case for [].reduce.

[].reduce starts at the beginning of the array and works its way to the end. [].reduceRight starts at the end and works its way to the beginning. I think the example for [].reduceRight used in the pen might be mildly confusing for those still learning since it reduces nested arrays. Here's an example of both using the same function; note how the output differs:

['a', 'b', 'c', 'd'].reduce((current, carry) => current+carry); // ➡ abcd
['a', 'b', 'c', 'd'].reduceRight((current, carry) => current+carry); // ➡ dcba

@sdras
Copy link
Owner

sdras commented Nov 13, 2018

Yeah @QWp6t you're right, I should make that a little more clear and easy to understand for beginners, since that's who this resource is for.

@rimildeyjsr
Copy link

@sdras - hello! Can I work on this issue? 🙂

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

No branches or pull requests

4 participants