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

Both the `reduce` and `filter` methods have not been called yet?

Hi! I've gotten an error message " Both the reduce and filter methods have not been called yet.", however, I didn't know what I was wrong. I checked my chords and I confirmed "groceryTotal" was 9.47. Can you advise me how to solve this, please? Thank you!

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(purchase => purchase.dept === "groceries").reduce((sum, purchase ) => sum + purchase.price, 0).toFixed(2);

1 Answer

Steven Parker
Steven Parker
207,988 Points

The challenge is expecting the result to be a number but "toFixed" converts it into a string. Just leave that off.

Thank you so much! I could solve this problem.