Build a Smart Pantry Restocker - Build a Smart Pantry Restocker

Tell us what’s happening:

Need Help, can’t meet condition:

  1. Your parseShipment function should convert shipment strings in the array into objects with the properties: sku, name, qty, expires, and zone.

  2. Duplicate SKUs in the shipment should be ignored.

  3. When zone is not present in a shipment string, you should assign it to general.

  4. The qty value should be converted into a number.

Your code so far

function parseShipment(rawData){
let result=[];
let usedSkus={};
for(let i=0;i<rawData.length;i++){
  let item=rawData[i].split(",")
  let sku=item[0].trim()
  if(usedSkus[sku]){
    continue;
  }
  usedSkus[sku] 

  let obj={
     sku:sku,
      name:item[1].trim(), 
      qty:Number(item[2].trim()), 
      expires:item[3].trim(), 
      zone:item[4]?item[4].trim():"general"
  }
  result.push(obj)
}
return result;
}

function planRestock(pantry, shipment){
let result=[];
for(let i=0;i<shipment.length;i++){
  let item=shipment[i];
  let found=false;
  for(let j=0;j<pantry.length;j++){
     if(pantry[j].name===item.name){
      found=true;
      break;
     }
  }
  if(item.qty<=0){
    result.push({
      type:"discard",
      item:item
    })
  }else if(found){
    result.push({
      type:"restock",
      item:item
    })
  }else{
    result.push({
      type:"donate",
      item:item
    })
  }
}
return result
}


function groupByZone(actions){
  let grouped={};

for(let i=0;i<actions.length;i++){
  let action=actions[i];
  let zone=action.item.zone
  if(!grouped[zone]){
    grouped[zone]=[]
  }
  grouped[zone].push(action)
}
return grouped
}


function clonePantry(pantry){
  let copy=[];
for(let i=0;i<pantry.length;i++){
  let item=pantry[i];
let obj={
   sku:item.sku, 
   name:item.name, 
   qty:item.qty, 
   expires:item.expires, 
   zone:item.zone
}
copy.push(obj)
}
return copy;
}


const rawShipment = [
  "sku1, Apples, 4, 2026-01-15, A1",
  "sku22, Bananas, 0, 2026-02-10, B2",
  "sku3, Mangoes, 3, 2026-03-01, A1"
];


const pantry = [
  {
    sku: "sku1",
    name: "Apples",
    qty: 5,
    expires: "2025-12-20",
    zone: "A1"
  },
  {
    sku: "sku4",
    name: "Rice",
    qty: 10,
    expires: "2027-01-01",
    zone: "C3"
  }
];



let parsedShipment=parseShipment(rawShipment);
let pantryCopy=clonePantry(pantry);
let actions=planRestock(pantryCopy, parsedShipment)
let groupResult=groupByZone(actions)

console.log(groupResult)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 Edg/148.0.0.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

Hi @DRAGOROD,

If you click on that GitHub link, you’ll see that the tests are using raw data like this:

[“A10|Tomatoes|5|2027-01-01|fridge”];

Happy coding