Build a Smart Pantry Restocker - Build a Smart Pantry Restocker

Tell us what’s happening:

I am stuck on test 16. I am not sure how we are supposed to use the functions work together. I thought they did. Is there something I am doing wrong?

Your code so far

function parseShipment(rawData) {
  const result = [];
  

  for (const data of rawData) {
    if (!data || !data.trim()) continue;
    
    const arrOfData = data.split("|");
    const [sku, name, qty, expires, zone = "general"] = arrOfData;
    const object = { sku, name, qty, expires, zone }
    object.qty = parseInt(object.qty);
    let exists = false;

    if (result.length === 0) {
      result.push(object);
      continue;
    }

    for (let item of result) {
      if (item.sku === object.sku) {
        exists = true;
        break;
      }
    }

    if (!exists) {
      result.push(object);
    }
  }

  return result;
}

function planRestock(pantry, shipment) {
  const actions = [];
  
  for (let i = 0; i < shipment.length; i++) {
    const action = {};
    const currentShipItem = shipment[i];
    let existsInPantry = false;

    if (currentShipItem.qty <= 0) {
      action.type = "discard";
      action.item = currentShipItem;
      actions.push(action);

      continue;
    }

    for (let j = 0; j < pantry.length; j++) {
      const currentPantryItem = pantry[j];
      if (currentPantryItem.sku === currentShipItem.sku) {
        existsInPantry = true;
        break;
      }
    }

    existsInPantry ? action.type = "restock" : action.type = "donate";

    action.item = currentShipItem;

    actions.push(action);
  }

  return actions;
}

function groupByZone(actions) {
  const result = {};

  for (const action of actions) {
    const key = action.item.zone;

    if (result[key]) {
      result[key].push(action);
    }
    else {
      result[key] = [];
    }
  }

  return result;
}

function clonePantry(pantry) {
  return structuredClone(pantry);
}



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", 
  "B21|Bananas|10|2027-01-01", 
  "C32|Eggs|3|2027-01-01|fridge", 
  "C32|Eggs|3|2027-01-01", 
  "D43|Pineapples|0|2027-01-01", 
  "E54|Peppers|-1|2027-01-01|fridge"
];

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

that last test is testing your functions work well in sequence, like

const shipment = parseShipment(rawData);
const pantryClone = clonePantry(pantry);
const actions = planRestock(pantryClone, shipment);
const grouped = groupByZone(actions);

you don’t need to have anything in particular in your code, only make sure that the functions each give an output that the next one can use

Hi @rayalva407 — @ILM is right that test 16 is about the functions working in sequence, and yours do pass data along correctly. The issue is hiding inside one of them: groupByZone.

Take a close look at your else branch:

js

else {
  result[key] = [];
}

When a zone shows up for the first time, you create an empty array for it… and then what happens to that action? Trace it step by step: on the first occurrence of a zone, does the current action ever actually get added to the array?

Compare it to your if branch, where you do result[key].push(action). Think about what the else branch needs to do after creating the new array — or how you could create the array already containing that first action.

That’s why the final grouped object comes out missing items. Fix that and run test 16 again. You’re really close!

Hi! I think the issue is in your groupByZone function.

When a zone does not exist yet, you create an empty array, but you don’t add the current action to it. So the first item for each zone is skipped.

Try creating the array first if needed, and then always pushing the action into it.Hope this helps!