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

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 21,632 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
231,261 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.