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

reduce().map() ? ex20 #150

Closed
511121418 opened this issue Jul 31, 2016 · 2 comments
Closed

reduce().map() ? ex20 #150

511121418 opened this issue Jul 31, 2016 · 2 comments

Comments

@511121418
Copy link

511121418 commented Jul 31, 2016

Issue 1 Description

During ex 20, when I checked the solution I noticed something weird, why using map() after this reduce ? this reduce doesn't return an array so it doesn't compile. (I included concatMap just to be sure).

Screenshoot

sans titre

Issue 2 Description

My solution doesn't valid though I got the right answer.

Screenshoot

sans titre

My Code

return movieLists
    .concatMap(movie => movie.videos
        .map(v => {
            v.boxarts = v.boxarts
                .reduce((a, b) => a.width * a.height < b.width * b.height ? a : b)
            return { id: v.id, title: v.title, boxart: v.boxarts.url}
        })
    );

Browser Information

  • Browser Name, Version: Firefox 47.0.1 and Google chrome
  • Operating System: win 7
  • Mobile, Desktop, or Tablet: Desktop

Hope this will help despite my bad english.
Have a nice day.

@rotexhawk
Copy link

Yes there seems to be a bug in exercise 20. It passes if you run it in the website but if you run it externally it will give you an error where that last .map() is.

@seanpoulter
Copy link
Contributor

CC: You can probably close this as a duplicate of #44 @morenoh149.


Good catch! It seems like a bug if you run reduce(...).map outside of the site but this is actually intentional. In Exercise 16 we write our own implementation of Array.prototype.reduce that returns the result in an Array. It has this little note:

Let's add a reduce() function to the Array type. Like map. Take note this is different from the reduce in ES5, which returns a value instead of an Array!

I found it confusing too. This was an intentional design choice that is discussed in #44. I'd summarize #44 by saying "we" decided to use the modified version of Array.prototype.reduce that returns an Array because it is similar to Observable.prototype.reduce returning an Observable. It's "more clear" to chain calls like reduce().map then it is to explain why the result types are different. (Personally I disagree with this approach. It'd be straightforward to explain we need to return an Array so we can chain the calls. 🤷‍♂️ )

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

No branches or pull requests

4 participants