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 trialCole Smith
Web Development Techdegree Student 13,802 Pointshow do I complete this callback function?
I cant tell if I'm using the right properties to accurately use these callback methods. I think I'm somewhat close but any hints would be appreciated, thanks.
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(grocItem => grocItem.dept === 'groceries')
.reduce((sum, grocItem) => sum + grocItem // this is where im stuck...) ;
console.log(groceryTotal);
1 Answer
Steven Parker
231,272 PointsYes, you were pretty close! But remember that after the filter, you still have an array of item objects, but you want to sum only their prices. Plus, you'll need to provide 0 for the initial value of sum:
// 👇 only sum prices
.reduce((sum, grocItem) => sum + grocItem.price, 0);
// 👆 starting with 0
Cole Smith
Web Development Techdegree Student 13,802 PointsCole Smith
Web Development Techdegree Student 13,802 Pointsok thanks Steven for clarifying. that's what it was, I had trouble figuring out how to solely add the prices , I always overthink these challenges ha. thank you for your help!