Build a Smart Pantry Restocker - Build a Smart Pantry Restocker: Test 7

Tell us what’s happening:

Hey y’all, MChance here.
Still need a little bit of practice to get back on track and I am, once again, stuck.
That planRestock function refuses to return my array of objects, no matter which loop I use. Where did I go wrong?
Thank you,
Happy coding!

Your code so far

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"
];
function parseShipment(rawData){
  const cleanDataArr = [];
  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";
    cleanDataArr.push(cleanItem);
    return cleanDataArr;
 }
for (let cleanItem of cleanDataArr){
    const itemIndex = cleanDataArr[cleanItem]
    if (cleanDataArr[itemIndex] === cleanDataArr[itemIndex+1]){
      cleanDataArr.splice(itemIndex+1);
   }
  }
 return cleanDataArr;
}
function planRestock(pantry, shipment){
 let parsedShipment = parseShipment(shipment);
 let array = [];
 for (let entry of parsedShipment) {
    let action = {type: "", item: entry};
    const pantryItem = pantry.find(p => p.sku === entry.sku);
    if (item.qty <= 0) {
      action.type = "discard";
    } 
    else if (pantryItem) {
      action.type = "restock";
    } else {
      action.type = "donate";
    }
     array.push(action);
   }
 return array;
}
  

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 OPR/131.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

try adding this below your function

const p = [{ sku: "A10", name: "Tomatoes", qty: 4, expires: "2027-01-01", zone: "fridge" }];
const s = [{ sku: "A10", name: "Tomatoes", qty: 6, expires: "2027-01-01", zone: "fridge" }];

const result = planRestock(p, s);

console.log(result)

see what errors appear in the console

Hey ILM,

Thank you for your quick answer.

I did as you told me and it the console says : TypeError: row.split is not a function which makes even more confused. row is a temp variable used only within the parseShipment function which passed its tests just fine.

Also, rawData is an array of strings, so it is iterable and each iteration is splittable,so I don’t quite see why I get this error. And I don’t get why the first function passed the tests either. It’s probably the most stupid mistake ever (knowing me) but I can’t put my finger on it.

What is it that I’m not seeing?

Thank you so much for your help!

MC

planRestock is not getting a rawData type string, so when you call parseShipment(shipment) inside planRestock, you get that error

I see it now. I was assuming the shipment parameter was in the same format as rawData, when in fact it’s already formatted as an array of objects. The test just passed.

It was indeed a very stupid mistake (like I said)

Thank you so much ILM

Happy Coding!

Happy coding