Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I got all steps checked except for 14th and I don’t know why. What I want from this help post is not the solution or a hint, but the reason why this happens because from my understanding, 14th step appeared to me like an overall validation check of other several steps in this lab if you get what I mean.

Your code so far

const exampleCargoManifest = {
    containerId: 1,
    destination: "Monterey, California, USA",
    weight: 831,
    unit: "lb",
    hazmat: false
};

function normalizeUnits(manifest) {
  const copy = structuredClone(manifest);

  if (manifest.unit === "kg") {
    return copy;
  }

  copy.weight *= 0.45;
  copy.unit = "kg";

  return copy;
}

function validateManifest(manifest) {
    let obj = {};

    for (const key in exampleCargoManifest) {
        if (!manifest.hasOwnProperty(key)) {
            obj[key] = "Missing";
        } else {
            switch (key) {
                case "containerId":
                  if (typeof manifest[key] !== "number" || Number.isNaN(manifest[key]) || (!Number.isInteger(manifest[key]) || manifest[key] < 1)) {
                        obj[key] = "Invalid"
                    }
                    break;
                case "destination":
                    if (typeof manifest[key] !== "string" || (typeof manifest[key] === "string" && manifest[key].trim().length === 0)) {

                        obj[key] = "Invalid";
                    }
                    break;
                case "weight":
                     if (typeof manifest[key] !== "number" || Number.isNaN(manifest[key]) || manifest[key] < 0) {
                        obj[key] = "Invalid"
                    }
                    break;
                case "unit":
                    if (typeof manifest[key] !== "string" || (manifest[key] !== "lb" && manifest[key] !== "kg"))
                        obj[key] = "Invalid";
                    break;
                case "hazmat":
                    if (typeof manifest[key] !== "boolean") {
                        obj[key] = "Invalid";
                    }
                    break;
            }
        }
    }

    return obj;
}

function processManifest(manifest) {
    let validationObj = validateManifest(manifest);
 if (Object.keys(validationObj).length === 0) {
        console.log(`Validation success: ${manifest.containerId}`);
        manifest = normalizeUnits(manifest);
        console.log(`Total weight: ${manifest.weight} kg`);
    } else {
        console.log(`Validation error: ${manifest.containerId}`);
        console.log(validationObj);
    }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

1 Like

Welcome to the forum @theynevermiss !

Should your code rely on a global variable?

If an expected key is missing in manifest, will this code ever find it?

Happy coding!

Hi @dhess ,

Thank you for your time and effort, I think I get what you mean, but to clarify I gotta ask;

Does this mean while 14th step’s tests are running, it checks for other properties that we don’t know about other than given ones in this lab?

The tests would only be checking for the expected properties in the manifest object – containerId, destination, weight, unit, hazmat. But if you pass in a manifest object that is missing one of those, how will your code ever find it if your logic is based only on keys in that object?

1 Like

Got it! Relying on a global varible seemed like a code smell from get-go anyways. I think I’ll try a different approach. Thanks again!