Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Combining Array Methods Nested Data and Additional Exploration

Sam Gord
Sam Gord
14,084 Points

please check my answer to the first challenge, did it another way.

const flatMovies = movies.reduce((array,current)=>{
    current.map(item => array.push(item));
    return array;
},[]);
...

1 Answer

Joel Kraft
STAFF
Joel Kraft
Treehouse Guest Teacher

Sam Gord, You got it! Nice work. This is a cool, creative solution.

When you're first learning it's important to just get the right answer. Then as you progress you'll naturally see ways to improve. There's a few things I notice about this solution, in case you're interested.

  1. You're not using the returned value of .map(). You could replace map with forEach and get the same result. For this solution, I would actually recommend using forEach to make the code easier to understand, since the use of the map method implies you're interested in the return value. Making code easy to understand is an important pursuit, as you or others revisit it weeks or even months later.
  2. This is sort of related, and you may have been aware of this given your code, but if you had used the return value of map here, you would have wound up with an array of numbers. That's because the push() method returns the length of the array its called on (though it might seem like it should return the array itself). Just a common gotcha of that method.

In general, there's a difference in schools of thought around the functional array methods in this course and methods like push. While the former (map, filter, etc) return a new array leaving the original array untouched, the latter(push, splice, etc) mutate or alter the original array. Proponents of the functional programming style prefer being able to see the progression of values in a program because it becomes easier to know what went wrong, if something ever does (and something always does). If you can see what the values were, and what they became, the problem becomes easier to trace, but if an array is being mutated, seeing that progression becomes much harder to do.

Anyway, I hope that makes sense and feels useful. Again, nice work!

Sam Gord
Sam Gord
14,084 Points

thank you so much for the answer \m/