Build a Smart Pantry Restocker - Build a Smart Pantry Restocker Test 10

Tell us what’s happening:

Not passing test 10 for planRestock() even though the correct type is being returned for each item in shipment. Specifying the last if statement parameters doesn’t work either. Not sure if my solution just isn’t the “correct way” or if I am genuinely missing some logic in my code.

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){
  let shipment = [];
  let copy = [];
  for (const item of rawData){    // for every item in the RawData array
    const itemArray = item.split("|");  // split on |
    let [sku, name, qty, expires, zone] = itemArray;  // destructure the array
    if (!copy.includes(sku)){   // if the copy array doesn't include the current sku:
      shipment.push({sku: sku, name: name, qty:Number(qty), expires: expires, zone: zone || "general"})   // push the destructured array to the shipment array
      copy.push(sku)        // then, push the current sku to copy
    }
  }
  return shipment
}

let shipment = parseShipment(rawData);
  // console.log(shipment);
  // tomatoes: restock
  // bananas: donate
  // eggs: donate
  // pineapples: discard
  // peppers: discard

function planRestock(pantry, shipment){
  let actions = [];
  for (const item of shipment){
    if (item.qty <= 0){
        actions.push({type: "discard", item});
        continue
    }
    for (let i = 0; i < pantry.length; i++){
      if (pantry[i].sku === item.sku){
        actions.push({type: "restock", item});
        break
      } else {
        actions.push({type: "donate", item});
        break
      }
    }
  }
  return actions
}

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

Your browser information:

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

Welcome to the forum @bluestJ,

What if there are no items in the pantry? Would your code donate all shipment items?

Do you really need that inner loop? Is there another way you can find a pantry item?

Happy coding

Great questions to think about! I looked back at the planRestock() functions specifications and noticed that there’s a possible inconsistency/ error. They are as follows:

  1. Otherwise, if the shipment item’s sku already exists in the pantry, the action type should be "restock".
  2. Otherwise (the shipment item’s sku does not exist in the pantry), the action type should be "donate"

Should “restock” and “donate” be swapped? I feel it would make more sense to restock an item not already in the pantry and to donate items coming in that you already have. I will try to adjust my code in the meantime to your original response, but I would appreciate some clarification here. Thanks!

Can you restock an item that’s not there? :slight_smile: