Build a Smart Pantry

Tell us what’s happening: I can’t figure out how to get rules 12 and 13 to work. I genuinely don’t know what this is asking me to do or how else to get the zone to sort the way it wants me to without modifying the restockplan function which it also didn’t like.

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 objectData = [];
  for (let i = 0; i < rawData.length; i++) {
    const itemData = rawData[i];
    const itemParsed = itemData.split("|");

    const itemObj = {
      sku: itemParsed[0],
      name: itemParsed[1],
      qty: parseInt(itemParsed[2]),
      expires: itemParsed[3],
      zone: "general"
    };

    if (itemParsed.length == 5) {
      itemObj.zone = itemParsed[4];
    }
    objectData.push(itemObj);    
  }

  for (let j = 1; j < objectData.length; j++) {
    const sku1 = objectData[j].sku;
    const sku2 = objectData[j-1].sku;
    
    if (sku1 === sku2) {
      objectData.splice(j, 1);
    }
  }

  return objectData

}

function planRestock(pantry, shipment) {
  const itemActions = [];

  for (let i = 0; i < shipment.length; i++) {
    const shipPlan = {
      type: "donate",
      item: shipment[i].name,
    };

    for (let j = 0; j < pantry.length; j++) {
      if (pantry[j].name === shipment[i].name) {
        shipPlan.type = "restock";
      };
    }
    itemActions.push(shipPlan);

    if (shipment[i].qty <= 0) {
      shipPlan.type = "discard";
    };
  }
  return itemActions;
}

function groupByZone(actions) {
  const parsedShipment = parseShipment(rawData);
  const actionZones = {
    fridge: [],
    general: [],
  }

  for (let i = 0; i < actions.length; i++) {
    for (let j = 0; j < parsedShipment.length; j++) {
      const zone = parsedShipment[j].zone
      if (actions[i].item == parsedShipment[j].name) {
        if (zone == "fridge") {
          actionZones.fridge.push(actions[i])
        } else {
          actionZones.general.push(actions[i])
        }
      }      
    }
  }
  return actionZones
}

function clonePantry(pantry) {
  const pantryClone = JSON.parse(JSON.stringify(pantry));
  return pantryClone;
}

const pantryClone = clonePantry(pantry);
const shipmentParsed = parseShipment(rawData);
const restockPlan = planRestock(pantryClone, shipmentParsed);
console.log(groupByZone(restockPlan));

Lesson URL

Hi @shawdowdancer14,

Please review User Story #2 again. planRestock should return an array of objects, each with two keys. Log what the function is returning. Does the item key contain “the parsed shipment object”?

Happy coding

I got closer, but now I’m running into [Object] issues. I tried to use JSON.stringify but it doesn’t format it the way the lab wants.

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 objectData = [];
  for (let i = 0; i < rawData.length; i++) {
    const itemData = rawData[i];
    const itemParsed = itemData.split("|");

    const itemObj = {
      sku: itemParsed[0],
      name: itemParsed[1],
      qty: parseInt(itemParsed[2]),
      expires: itemParsed[3],
      zone: "general"
    };

    if (itemParsed.length == 5) {
      itemObj.zone = itemParsed[4];
    }
    objectData.push(itemObj);    
  }

  for (let j = 1; j < objectData.length; j++) {
    const sku1 = objectData[j].sku;
    const sku2 = objectData[j-1].sku;
    
    if (sku1 === sku2) {
      objectData.splice(j, 1);
    }
  }

  return objectData

}

function planRestock(pantry, shipment) {
  const itemActions = [];

  for (let i = 0; i < shipment.length; i++) {
    const shipPlan = {
      type: "donate",
      item: shipment[i],
    };

    for (let j = 0; j < pantry.length; j++) {
      if (pantry[j].name === shipment[i].name) {
        shipPlan.type = "restock";
      };
    }
    itemActions.push(shipPlan);

    if (shipment[i].qty <= 0) {
      shipPlan.type = "discard";
    };
  }
  return itemActions;
}

function groupByZone(actions) {
  const actionZones = {
    fridge: [],
    general: [],
  }

  for (let i = 0; i < actions.length; i++) {
    if (actions[i].item.zone == "fridge") {
      actionZones.fridge.push(actions[i]);
    } else {
      actionZones.general.push(actions[i]);    
    }
  }
  return actionZones
}


function clonePantry(pantry) {
  const pantryClone = JSON.parse(JSON.stringify(pantry));
  return pantryClone;
}

const pantryClone = clonePantry(pantry);
const shipmentParsed = parseShipment(rawData);
const restockPlan = planRestock(pantryClone, shipmentParsed);
console.log(groupByZone(restockPlan));

that is a limitation of the environment, you would not see that if you use console.log like this

console.log(JSON.stringify(groupByZone(restockPlan)));

or if you run your code in a different environment

That’s not the issue. You will see the entire object in your browser’s console.

The issue is that you are hard coding zones based on expected values, which makes your code inflexible.

What if there is a “deep freeze” zone? Could your code handle that?

Happy coding