JavaScript JavaScript Array Iteration Methods Combining Array Methods Combining filter() and reduce()

Joe Mbome
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joe Mbome
Front End Web Development Techdegree Graduate 16,421 Points

Using the filter and reduce methods on the purchaseItems array, add the total price of all the groceries (items with a d

please need some help with this!

app.js
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(items=>items.dept==='groceries')
.reduce((sum,items)=>sum + items, 0);

2 Answers

Robert Manolis
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Robert Manolis
Treehouse Guest Teacher

Hey Joe Mbome , try sum + items.price in the reduce function. And don't forget, you can also log those parameter values out inside of those functions to makes sure they are what you think they are before you try using them. :thumbsup: