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 trialIstvan Toth
14,348 PointsWhy 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)
1 Answer
Steven Parker
231,268 PointsYou 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.
Trent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 PointsTrent Stenoien
Full Stack JavaScript Techdegree Graduate 21,632 PointsReduce 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)