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