Tell us what’s happening:
If remove the negative/zero qty items, then even test 12 fails.
As of now 13,17 & 18 are failing.
What am I doing wrong ?
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 returnObject = [];
for (const item of rawData) {
const details = item.split("|");
if (!returnObject.some((obj) => obj.sku === details[0].trim())) {
returnObject.push({
sku: details[0].trim(),
name: details[1].trim(),
qty: Number(details[2]),
expires: details[3].trim(),
zone: details[4]
? details[4].trim()
? details[4].trim()
: "general"
: "general",
});
}
}
return returnObject;
}
function planRestock(pantry, shipment) {
const actions = [];
for (const item of shipment) {
let type = "";
if (item.qty <= 0) {
type = "discard";
} else if (
pantry.findIndex((pantryItem) => pantryItem.sku === item.sku) >= 0
) {
type = "restock";
} else {
type = "donate";
}
actions.push({ type, item });
}
return actions;
}
function groupByZone(actions) {
let groupedByZone = {};
for (const entry of actions) {
if (!groupedByZone[entry.item.zone]) {
groupedByZone[entry.item.zone] = [];
}
groupedByZone[entry.item.zone].push(entry.item);
}
return groupedByZone;
}
function clonePantry(pantry) {
return structuredClone(pantry);
}
console.log(groupByZone(planRestock(pantry,parseShipment(rawData))))
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:155.0) Gecko/20100101 Firefox/155.0
Challenge Information:
Build a Smart Pantry Restocker - Build a Smart Pantry Restocker