Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

ludopuig
seal-mask
.a{fill-rule:evenodd;}techdegree
ludopuig
Full Stack JavaScript Techdegree Student 6,436 Points

not working

Hi,

I have been trying to make this work buy I am stuck... I don't see what is wrong so a hint would be highly appreciated... it gives the following error:

"Both the reduce and filter methods have not been called yet."

Any idea?

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

let productsFromGrocery = dept => purchaseItems['dept'] === 'groceries';
let total = (sum, product) => sum + product['price'];

groceryTotal = purchaseItems
  .filter(productsFromGrocery)
  .reduce(total, 0)
  .toFixed(2);

2 Answers

Hello! I have an answer for ya. Okay, so I have the answer as to why yours failed. However, itll look confusing. Afterward, I provided a tip on how to keep it from being confusing in the future. :) Its kind of a naming convention.

First thing is that you called the wrong object inside of the "productsFromGrocey" variable. I found this out by testing the end result and saw your end result returned 0.00. I kinda cheated. lol. Tested on JsFiddle

Your code:

let productsFromGrocery = dept => purchaseItems['dept'] === 'groceries';

Should be:

let productsFromGrocery = dept => dept['dept'] === 'groceries';

The reason why you dont have to declare which object to pull the the "dept" from is because you are basically saying "For each object/array(key:value pair) inside of the object to be filtered(object.filter() or even better purchaseItems.filter()), we want to place in a temporary local variable named dept and for each dept, we want to grab the value for the key "dept". Move on to my next comment if this is confusing. :)

SECOND...Lets make this less confusing... Instead of naming the key/value pair the same name as the key you are searching for, why not name it something more relevant to the entire object? Like this...

let productsFromGrocery = item => item['dept'] === 'groceries';

Notice, I avoided calling it "product." In Javascript, and all programming, product can mean a lot of things but usually refers to a math term. For this reason, I try to only use words that will not likely conflict but also are easy to read and describe the object. You could have also called it "thing" or "storeItem" or even "profitMaker" though I cant think of a better term than "item" right now.

BONUS! Ive included a passing answer to that quiz that tries to keep it simple. :) I did this because you already understand the logic, you just got tripped up on the wording in your code and lost your place, I think. Enjoy!

My code[Left out the part we shouldnt change ;)]:

// groceryTotal should be: 9.47
// Write your code below

groceryTotal = purchaseItems.filter(item => item.dept == "groceries").reduce((sum, product) => sum + product.price, 0);
ludopuig
seal-mask
.a{fill-rule:evenodd;}techdegree
ludopuig
Full Stack JavaScript Techdegree Student 6,436 Points

EyAaron: clap, clap, clap, clap, clap, clap, clap, clap, clap, clap, clap, clap

Thank you very much, very clear answer!!!