Build a Smart Pantry Restocker - Build a Smart Pantry Restocker

Tell us what’s happening:

I had to verify the github link submitted in another hint; however, the problem is that the structure of the actions array elements is different from any result of a function. Please advise I dont know how to proceed

Your code so far

function parseShipment(rawData) {
    let result = [];
    let skus = []
    for (let element of rawData) {
        let separated = element.split("|");
        if (skus.includes(separated[0])) {
            continue
        }
        let newObj = {};
        newObj.sku = separated[0];
        skus.push(separated[0]);
        newObj.name = separated[1];
        newObj.qty = Number(separated[2]);
        newObj.expires = separated[3];
        separated.length === 5 ? newObj.zone = separated[4] : newObj.zone = "general";
        result.push(newObj)
    }
    return result
}

function planRestock(pantry, shipment) {
    let result = [];
    let skus = [];
    for (let product of shipment) {
        let newObj = {};
        if (product.qty <= 0) {
            newObj.type = "discard";
            newObj.item = product.name;
            result.push(newObj);
            continue
        }
        else if (pantry.length === 0 && product.qty > 0) {
            newObj.type = "donate";
            newObj.item = product.name;
            result.push(newObj);
            continue
        }
        for (let item of pantry) {
            if (product.sku === item.sku) {
                newObj.type = "restock";
                newObj.item = product.name;
                result.push(newObj);
                break;
            }
        }
    }
    return result
}


function groupByZone(actions) {
    let result = {};
    for (let action of actions) {
        let zone = action.item.zone;
        if (!result.hasOwnProperty(zone)) {
            result[zone] = [];
            result[zone].push(action)
            continue
        }
        result[zone].push(action)
    }
    return result
}

function clonePantry(pantry) {
  return structuredClone(pantry)
}

const rawData = ["A10|Tomatoes|5|2027-01-01|fridge"];
const pantry = [{ sku: "A10", name: "Tomatoes", qty: 4, expires: "2027-01-01", zone: "fridge" }];
let shipment = parseShipment(rawData);
let clonedPantry = clonePantry(pantry);
let plannedRestock = planRestock(clonedPantry, shipment);
console.log(groupByZone(plannedRestock))

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

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-smart-pantry-restocker/69a5f35669099ed52f8563b1.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @JeremiG936,

If you test with the following data, you’ll see that your planRestock function is not addressing items that need to be donated:

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", // Restock existing item
  "B21|Bananas|10|2027-01-01", // Donate new item without zone
  "C32|Eggs|3|2027-01-01|fridge", // Donate to a defined zone
  "C32|Eggs|3|2027-01-01", // Duplicated SKU in shipment
  "D43|Pineapples|0|2027-01-01", // Discard with quantity of 0
  "E54|Peppers|-1|2027-01-01|fridge" *// Discard even if it's not in pantry*
];

Happy coding