Build a Smart Pantry Restocker - Build a Smart Pantry Restocker

Tell us what’s happening:

Hi good people, I need some help. I don’t understand the 16th and 17th steps for the assignment: The functions should work together to process a shipment and group the resulting actions/You should log the resulting actions grouped by zones.

Your code so far


function parseShipment(rawData){
  const cleanDataArr = [];
  const skus = [];
  for(let row of rawData){
    const cleanItem = {};
    const arr = row.split('|');
    cleanItem.sku = arr[0].trim();
    cleanItem.name = arr[1].trim();
    cleanItem.qty = Number(arr[2]);
    cleanItem.expires = arr[3].trim();
    cleanItem.zone = arr[4]? arr[4].trim(): "general";
  
    //console.log(skus);
    if(!skus.includes(cleanItem.sku)) {
    cleanDataArr.push(cleanItem);
    skus.push(cleanItem.sku);
    } 
    
  }
  return cleanDataArr;
}
function planRestock(pantry, shipment) {
  const actions = [];
  for (const item of shipment) {
    if (item.qty <= 0) {
      actions.push({
        type: "discard",
        item
      });
    } else if (pantry.some(p => p.sku === item.sku)) {
      actions.push({
        type: "restock",
        item
      });
    } else {
      actions.push({
        type: "donate",
        item
      });
    }
  }
  return actions;
}
function groupByZone(actions){
  let groupedActions = {};
  for (const action of actions) {
    const zone = action.item.zone;
    if(!groupedActions[zone]){
      groupedActions[zone] = [];
    }  groupedActions[zone].push([action.item.sku,action.type]);
  }
  return groupedActions;
}
function clonePantry(pantry){
  const clonedPantry = JSON.parse(JSON.stringify(pantry));

  return clonedPantry;
}
const pantry = [
  { sku: "A10", name: "Tomatoes", qty: 4, expires: "2027-01-01", zone: "fridge" },
  { sku: "D43", name: "Pineapples", qty: 2, expires: "2020-01-01", zone: "general" }
];

const rawData = [
  "A10|Tomatoes|5|2027-01-01", // Restock existing item
  "B21|Bananas|10|2027-01-01", // Donate new item without zone
  "C32|Eggs|3|2027-01-01|fridge", // Donate to a defined zone
  "C32|Eggs|3|2027-01-01", // Duplicated SKU in shipment
  "D43|Pineapples|0|2027-01-01", // Discard with quantity of 0
  "E54|Peppers|-1|2027-01-01|fridge" // Discard even if it's not in pantry
];
const shipment = parseShipment(rawData);
const pantryCopy = clonePantry(pantry);
const actions = planRestock(pantryCopy, shipment);
const grouped = groupByZone(actions);

console.log(grouped);





Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36

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 @hkabeya

User stories will be updated soon where a good description will be provided about actions, thanks to this PR.

Meanwhile check the following description out and let me know if you still have any problems.

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: [...] }.