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 Combining filter() and reduce()

Istvan Toth
Istvan Toth
13,172 Points

Why do we need filter to make things more complicated if reduce does the job by itself?

const pricesOver10 = products.reduce((pricesAll,price) => {if(price.price > 10){pricesAll += price.price}return pricesAll},0).toFixed(2)

Trent Stenoien
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 20,034 Points

Reduce adds up the sum, filter replaces your 'if' statement. My syntax might be not be perfect, but this is the idea.

const pricesOver10 = products.filter(price.price > 10).reduce((pricesAll,price) => pricesAll += price.price, 0).toFixed(2)

1 Answer

Steven Parker
Steven Parker
221,902 Points

You might not have used the .filter method, but you've recreated its functionality in your conditional code. While this is a technically legitimate solution, it's less compact than the video example and requires about 50% more space.