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 trialSteven mudie
5,523 Pointsbug in the learning material
Both the
reduce
andfilter
methods have not been called yet.
here is my code:
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(item => item.dept === "groceries")
.reduce((total,item) => {
return total += item.price;
}, 0).toFixed(2);
1 Answer
Steven Parker
231,269 PointsApparently, the toFixed
is causing the issue by converting the result into a string. It wasn't part of the instructions, and the validator is expecting groceryTotal
to be a number.
Steven mudie
5,523 PointsSteven mudie
5,523 Pointsthanks, removed and it worked..
but the comment says the price should be "9.47" so I prefixed it to 2. Also we learned how to prefix in the previous video in the module so I thought it was necessary.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsI can see how that could be confusing. You might drop a note to the Support folks with a suggestion about clarifying the objective.