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 trialMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsAbout reduce() and filter()
I have a question again, I dont know how this work. But it seems really close to success, just dont know why where is the error.
Hope someone can help me out,thank you so much!
const purchaseItems = [
{
name: 'apples',
dept: 'groceries',
price: 2.49
},
{
name: 'bread',
dept: 'groceries',
price: 2.99
},
{
name: 'batteries',
dept: 'electronics',
price: 5.80
},
{
name: 'eggs',
dept: 'groceries',
price: 3.99
},
{
name: 't-shirts',
dept: 'apparel',
price: 9.99
}
];
let groceryTotal;
// groceryTotal should be: 9.47
// Write your code below
groceryTotal = purchaseItems.filter(fi=>{fi.dept == 'groceries'})
.reduce((acc,ui) => acc+ui.price,0);
1 Answer
Steven Parker
231,269 PointsYou're really close, but an arrow function that has a body in braces needs an explicit "return" statement to pass back a value. Here you can use the abbreviated form which returns implicitly:
/*...*/ .filter(fi => fi.dept == 'groceries')
MIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsOh, I dont know still. So, within the arrow function the difference between curly braces and without curly braces is return and no return value, right?
Thank you so much!
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYes, one statement with no braces is automatically returned. But a function body in braces must call "return" explicitly.
MIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsThank you so much!