Tell us what’s happening:
I am stuck on test 16. I am not sure how we are supposed to use the functions work together. I thought they did. Is there something I am doing wrong?
Your code so far
function parseShipment(rawData) {
const result = [];
for (const data of rawData) {
if (!data || !data.trim()) continue;
const arrOfData = data.split("|");
const [sku, name, qty, expires, zone = "general"] = arrOfData;
const object = { sku, name, qty, expires, zone }
object.qty = parseInt(object.qty);
let exists = false;
if (result.length === 0) {
result.push(object);
continue;
}
for (let item of result) {
if (item.sku === object.sku) {
exists = true;
break;
}
}
if (!exists) {
result.push(object);
}
}
return result;
}
function planRestock(pantry, shipment) {
const actions = [];
for (let i = 0; i < shipment.length; i++) {
const action = {};
const currentShipItem = shipment[i];
let existsInPantry = false;
if (currentShipItem.qty <= 0) {
action.type = "discard";
action.item = currentShipItem;
actions.push(action);
continue;
}
for (let j = 0; j < pantry.length; j++) {
const currentPantryItem = pantry[j];
if (currentPantryItem.sku === currentShipItem.sku) {
existsInPantry = true;
break;
}
}
existsInPantry ? action.type = "restock" : action.type = "donate";
action.item = currentShipItem;
actions.push(action);
}
return actions;
}
function groupByZone(actions) {
const result = {};
for (const action of actions) {
const key = action.item.zone;
if (result[key]) {
result[key].push(action);
}
else {
result[key] = [];
}
}
return result;
}
function clonePantry(pantry) {
return structuredClone(pantry);
}
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"
];
const shipment = parseShipment(rawData);
const pantryCopy = clonePantry(pantry);
const actions = planRestock(pantryCopy, shipment);
const grouped = groupByZone(actions);
console.log(grouped);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Build a Smart Pantry Restocker - Build a Smart Pantry Restocker