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

My solution works, check fails it however.

Trying to pass this Challenge, I submit the attache code, using mehtod chaining, arrow functions etc as we've been learning. The check for my code fails despite it working as expected. What is this challenge looking for if not an answer that reflects the tutorials?

I should point out that I replcaed the code thinking it might be looking for a longhand version of the functions.

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( item => item.dept === 'groceries' )
                .reduce( (total, item) => total + item.price, 0)
                .toFixed(2);

console.log(groceryTotal);

//Outputs 9.47 as required
wc93
wc93
25,711 Points

Same thing happening to me. I ended up removing the toFixed but the toFixed worked fine in the workspace.

""" groceryTotal = purchaseItems.filter(item => item.dept === 'groceries') .reduce((sum, item) => sum + item.price, 0).toFixed(2);

console.log(groceryTotal) """

2 Answers

Fran ADP
Fran ADP
6,291 Points

the toFixed, it will change the number t a string.

It's because of the toFixed(2), this will change the number to be a string.

Nope, this is the reply I get despite both methods being called, with or without toFixed() :

"Bummer: Both the reduce and filter methods have not been called yet" Seems odd to me.

you sure? I copypasted your code and removed the toFixed(2) and it passed for me.

Hmm, a browser refresh seems to have sorted it out. Even after removing toFixed(2) it still failed, duimped the session and it does work. sigh time for a coffee I think.

Thanks Zimri