Build a Smart Pantry Restocker - Build a Smart Pantry Restocker

Tell us what’s happening:

I am trying to create the groupByZone function calling it like so:

const actions = [
  { type: "restock", item: { sku: "A1", zone: "fridge" } },
  { type: "donate",  item: { sku: "B1", zone: "fridge" } },
  { type: "discard", item: { sku: "C1", zone: "pantry" } },
];
console.log(const groupByZone(actions));

and am getting what I think is a correct result:

{ fridge: [ 'restock', 'donate' ], pantry: [ 'discard' ] }

But I get this error:

13. Your groupByZone function should correctly group actions with the right content and count.

What does it mean by count?

The instructions for this function are

  1. You should implement a groupByZone(actions) function that groups the actions into storage zones based on each item’s zone property.
    The function should return an object where each key is a zone name and the value is an array of actions belonging to that zone.
    For example, if actions contain items with zones “fridge” and “pantry”, the result should be { fridge: […], pantry: […] }.

No mention of count. Please advise.

Your code so far

const groupByZone = actions => {
  const retObj = {};
  actions.forEach(action => {
    if (retObj.hasOwnProperty(action.item.zone)) {
      retObj[action.item.zone].push(action.type);
    } else {
      retObj[action.item.zone] = [action.type];
    }
  })
  return retObj;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0

Challenge Information:

Build a Smart Pantry Restocker - Build a Smart Pantry Restocker

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-smart-pantry-restocker/69a5f35669099ed52f8563b1.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hey @ambradnum

User story 3 tells that the function should return an object where each key is a zone and the value of array is an array of actions belonging to that zone.

You did the first part correct and now you need to do the second part. Second part talks about array of actions belonging to that zone, but if I look your output, all I see is the type from those actions.

Thanks for replying @imsomilg .

My output is { fridge: [ 'restock', 'donate' ], pantry: [ 'discard' ] }

Isn’t the array [ 'restock', 'donate' ] an array of actions for fridge and [ 'discard' ] the array for pantry?

Try to log actions and check the output and share it over here.

Here is the code:

const groupByZone = actions => {
  const retObj = {};
  console.log(actions);
  console.log("---");
  actions.forEach(action => {
    if (retObj.hasOwnProperty(action.item.zone)) {
      retObj[action.item.zone].push(action.type);
    } else {
      retObj[action.item.zone] = [action.type];
    }
  })
  return retObj;
}

and here is the output:

[ { type: 'restock', item: { sku: 'A1', zone: 'fridge' } },
  { type: 'donate', item: { sku: 'B1', zone: 'fridge' } },
  { type: 'discard', item: { sku: 'C1', zone: 'pantry' } } ]
---
{ fridge: [ 'restock', 'donate' ], pantry: [ 'discard' ] }

Read user story 2 carefully, it states that item is the parsed shipment object, but your item only includes sku and zone.

I’m talking about user story 3 which doesn’t mention shipmentanywhere

In order to get to user story 3, you need to make sure user story 2 is done properly. User story 2 provides us with actions array which is later used in groupByZone.

Your current actions array is incorrect and in order to pass user story 3, you need to make sure you have the correct actions array coming from above.

Here is my full code:


const parseShipment = rawData => {
  const retArr = [];
  for (let rawShipment of rawData) {
    const shipment = rawShipment.split('|');
    const sku = shipment[0];
    if (!retArr.find(s => s.sku === sku)) {
      const obj = {
        sku:     sku,
        name:    shipment[1],
        qty:     +shipment[2],
        expires: shipment[3],
        zone:    shipment[4] || 'general'
                  }
      retArr.push(obj);
    }
  }
  return retArr;
}

const planRestock = (pantry, shipment) => {
  const retArr = [];
  shipment.forEach(s => {
    let type = "";
    if (s.qty <= 0) {
      type = "discard";
    } else if (pantry.find(s => s.sku === s.sku)) {
      type = "restock";
    } else {
      type = "donate";
    }
    retArr.push({type: type, item: s});
  });
  return retArr;
}

const groupByZone = actions => {
  const retObj = {};
  console.log(actions);
  console.log("---");
  actions.forEach(action => {
    if (retObj.hasOwnProperty(action.item.zone)) {
      retObj[action.item.zone].push(action.type);
    } else {
      retObj[action.item.zone] = [action.type];
    }
  })
  return retObj;
}

const clonePantry = pantry => {
  const retArr = [];
  return retArr;
}

const rd = [
  "A10|Tomatoes|5|2027-01-01",        // no zone field
  "B21|Bananas|10|2027-01-01|fridge", // zone: "fridge"
  "C32|Eggs|3|2027-01-01|pantry",     // zone: "pantry"
];

console.log("parseShipment");
const pantry = parseShipment(rd);
console.log(pantry);

const rd2 = [
  "B21|Bananas|0|2027-01-01|fridge",
  "C32|Eggs|3|2027-01-01|pantry"
];

const shipment = parseShipment(rd2);
console.log("planRestock");
console.log(planRestock(pantry, shipment));

console.log("groupByZone");
console.log(groupByZone(planRestock(pantry, shipment)));

I still get the same results though (the returned object seems to be correct):


parseShipment
[ { sku: 'A10',
    name: 'Tomatoes',
    qty: 5,
    expires: '2027-01-01',
    zone: 'general' },
  { sku: 'B21',
    name: 'Bananas',
    qty: 10,
    expires: '2027-01-01',
    zone: 'fridge' },
  { sku: 'C32',
    name: 'Eggs',
    qty: 3,
    expires: '2027-01-01',
    zone: 'pantry' } ]
planRestock
[ { type: 'discard',
    item: 
     { sku: 'B21',
       name: 'Bananas',
       qty: 0,
       expires: '2027-01-01',
       zone: 'fridge' } },
  { type: 'restock',
    item: 
     { sku: 'C32',
       name: 'Eggs',
       qty: 3,
       expires: '2027-01-01',
       zone: 'pantry' } } ]
groupByZone
[ { type: 'discard',
    item: 
     { sku: 'B21',
       name: 'Bananas',
       qty: 0,
       expires: '2027-01-01',
       zone: 'fridge' } },
  { type: 'restock',
    item: 
     { sku: 'C32',
       name: 'Eggs',
       qty: 3,
       expires: '2027-01-01',
       zone: 'pantry' } } ]
---
{ fridge: [ 'discard' ], pantry: [ 'restock' ] }

I also get the same error message about step 13 but no errors on steps 1 to 12

Well congrats on clearing User Story 2 properly. Now I can see proper actions array.

Now, coming to User Story 3, it stats that function should return an object where

  1. each key is a zone name
  2. value of they key should be an array of actions belonging to that zone.

You did 1st part right but when it comes to the second part, I don’t see any actions array for any zone. All I see is [ 'discard' ] and [ 'restock' ].

I changed the ‘input’ into planRestock to get a slightly different array being passed into groupByZone:

const rd2 = [
  "A10|Tomatoes|5|2027-01-01|fridge",
  "B21|Bananas|0|2027-01-01|fridge",
  "C32|Eggs|3|2027-01-01|pantry"
];

Now my output from groupByZone is:

CONSOLE LOGGING ACTIONS:
[ { type: 'restock',
    item: 
     { sku: 'A10',
       name: 'Tomatoes',
       qty: 5,
       expires: '2027-01-01',
       zone: 'fridge' } },
  { type: 'discard',
    item: 
     { sku: 'B21',
       name: 'Bananas',
       qty: 0,
       expires: '2027-01-01',
       zone: 'fridge' } },
  { type: 'restock',
    item: 
     { sku: 'C32',
       name: 'Eggs',
       qty: 3,
       expires: '2027-01-01',
       zone: 'pantry' } } ]
---
RETURN FROM groupByZone:
{ fridge: [ 'restock', 'discard' ], pantry: [ 'restock' ] }

Aren’t [ 'restock', 'discard' ] and [ 'restock' ] arrays of actions?

They are just the type of action in actions array but if you read the User Story 3 carefully, it is asking array of actions (not only type property,but the whole action) belonging to that zone as the value of the zone name

If you look at the actions passed in you see there are restock types for A10 (zone fridge) and C32 (zone pantry) and a discard type for B21 (zone fridge).

The output from groupByZone shows a zone name of fridge with the value of an array of actions ('restock' and 'discard') and a zone name of pantry with the value of an array of one action ('restock'). Isn’t that what is required? If not please could you tell me exactly what the output should be for my actions input into groupByZone so I can see where I am going wrong.

Are you sure that is an array of actions? Check the console.log(actions), over there you would see multiple elements, those elements in a group is called actions. Rather than giving the actual element from actions, you are just giving the value of key type.

Hi @ambradnum,

Please review User Story #4 to make sure you are implementing it correctly.

Happy coding

Thank you so much @imsomilg - I just had to remove .type as you explained. It works!! - now on to the next step :slight_smile:

Was trying to solve groupByZone first