ReactiveX / learnrx

A series of interactive exercises for learning Microsoft's Reactive Extensions Library for Javascript.
1.4k stars 292 forks source link

Question 12 can be solved with .toString() #139

Closed fokusferit closed 5 years ago

fokusferit commented 8 years ago

I've tried the following code:

  return movieLists.map(function(item){
    return item.videos.map(function(video){
      return {id: video.id, title: video.title, boxart: video.boxarts.filter(function(art){return (art.width === 150 && art.height === 200);}).map(function(art){return art.url}).toString()};
    });
  }).concatAll();

I don't know if that should be allowed. I'm new to the whole functional paradigm and I guess this shouldn't be possible?

If not, it's maybe great to know another solution without a second inner .concatAll()

morenoh149 commented 8 years ago

please use backticks () toget code highlighting`

seanpoulter commented 5 years ago

Your solution worked since:

["1","2","3"].toString()

returns

"1,2,3"

Here's your solution with a few comments showing how the data was transformed:

  return movieLists.map(function(item){
    return item.videos.map(function(video){
      return {
        id: video.id,
        title: video.title,
        boxart: video.boxarts.
          filter(function(art){return (art.width === 150 && art.height === 200);}).
          map(function(art){return art.url}).
          // 👆 Map transforms an Array and returns an Array:
          // [{ url: "str", ... }] -> ["str"]
          toString()
          // Calling `.toString()` here removes the square brackets `[]` to be "str"
      };
    });
  }).concatAll();


and the comparison between the actual and expected results are performed by comparing the values as strings. Issue #7 suggests changing this behaviour.


I think you can close this one as a duplicate @morenoh149.