镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

reduceRight wrong example? #78

Open

Description

@alizhdanov

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?

Activity

  1. QWp6t commented on Nov 12, 2018

    @QWp6t

    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
  2. sdras commented on Nov 13, 2018

    @sdras
    Owner

    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.

  3. rimildeyjsr commented on Oct 2, 2019

    @rimildeyjsr

    @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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions