Tell us what’s happening:
All tests are passing except for 17.
I have used all of the functions and the items are grouped by zones and are being printed to the console so I am not sure what is 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) {
let output = [];
outerFor: for (let i = 0; i < rawData.length; i++) {
let data = rawData[i].split("|");
//convert qty value
data[2] = parseInt(data[2]);
//default zone to general
if (data.length == 4) {
data[4] = "general";
}
//skip duplicate sku
if (output.length > 0) {
for (let item of output) {
if (item.sku == data[0]) {
continue outerFor;
}
}
}
output.push({
sku: data[0],
name: data[1],
qty: data[2],
expires: data[3],
zone: data[4]
});
}
return output;
}
function planRestock(pantry, shipment) {
let actions = [];
for (let item of shipment) {
if (item.qty <= 0) {
actions.push({
type: "discard",
item: item});
continue;
}
for (let pantryItem of pantry) {
if (item.sku == pantryItem.sku) {
actions.push({
type: "restock",
item: pantryItem});
continue;
}
}
actions.push({
type: "donate",
item: item});
}
return actions;
}
function groupByZone(actions) {
let zones = {};
for (let action of actions) {
if (Object.hasOwn(zones, action.item.zone) == false) {
zones[action.item.zone] = [];
zones[action.item.zone].push(action);
} else {
zones[action.item.zone].push(action);
}
}
return zones;
}
function clonePantry(pantry) {
let newPantry = [];
for (let item of pantry) {
let newPantryItem = {
sku: item.sku,
name: item.name,
qty: item.qty,
expires: item.expires,
zone: item.zone
}
newPantry.push(newPantryItem);
}
return newPantry;
}
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; rv:155.0) Gecko/20100101 Firefox/155.0
Challenge Information:
Build a Smart Pantry Restocker - Build a Smart Pantry Restocker