Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Sam Gord
Sam Gord
14,060 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,060 Points

thank you so much for the answer \m/