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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsProblem with the reducer method for getting total of groceries
I'm pretty sure the problem is with the filter method but all I'm getting from the code challenge is "Unexpected token ." So I think I'm close but I'm out of ideas on this one :)
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(department => purchaseItems.department == "groceries")
.reduce(sum, department) => sum + purchaseItems.price, 0
.toFixed(2);
1 Answer
Matt Brock
28,330 PointsYou were missing the parentheses around the guts of the .reduce()
function and your arguments were just a little off:
groceryTotal = purchaseItems
.filter(item => item.dept == 'groceries')
.reduce((sum, item) => sum + item.price, 0)
.toFixed(2);
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHaving tested this code on a local file I believe that code should be passing.
I'm getting an error
Both the `reduce` and `filter` methods have not been called yet.
Joel Kraft would you be able to check it out and confirm? :)
Matt Brock
28,330 PointsMatt Brock
28,330 PointsFor whatever reason, removing the
.toFixed()
method passed the challenge: