Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I am just new to coding and have been stuck on this lab for over a week. I don’t mind the struggle but I am stumped on why I keep failing test #3 and #14.

Your code so far

const manifest = {};

function normalizeUnits(manifest) {
  const correction = { ...manifest };
  if (manifest.unit !== "kg") {
    correction.weight = manifest.weight * 0.45;
    correction.unit = "kg"; 
    return correction;
  } else return manifest;
}

function validateManifest(manifest) {
  const valid = {};
  const missInv = {};
  let errorCount = 0;
  if (manifest.containerId === undefined) {
    missInv.containerId = "Missing";
    errorCount++;
  } else if (
    manifest.containerId <= 0 ||
    !Number.isInteger(manifest.containerId) ||
    typeof manifest.containerId !== "number" ||
    isNaN(manifest.containerId)
  ) {
    missInv.containerId = "Invalid";
    errorCount++;
  }
  if (manifest.destination === undefined) {
    missInv.destination = "Missing";
    errorCount++;
  } else if (
    typeof manifest.destination !== "string" ||
    manifest.destination.trim() === ""
  ) {
    missInv.destination = "Invalid";
    errorCount++;
  }
  if (manifest.weight === undefined) {
    missInv.weight = "Missing";
    errorCount++;
  } else if (
    manifest.weight <= 0 ||
    typeof manifest.weight !== "number" ||
    Number.isNaN(manifest.weight)
  ) {
    missInv.weight = "Invalid";
    errorCount++;
  }
  if (manifest.unit === undefined) {
    missInv.unit = "Missing";
    errorCount++;
  } else if (
    manifest.unit.toLowerCase() !== "lb" &&
    manifest.unit.toLowerCase() !== "kg"
  ) {
    missInv.unit = "Invalid";
    errorCount++;
  }
  if (manifest.hazmat === undefined) {
    missInv.hazmat = "Missing";
    errorCount++;
  } else if (typeof manifest.hazmat !== "boolean") {
    missInv.hazmat = "Invalid";
    errorCount++;
  }
  if (errorCount > 0) {
    return missInv;
  } else return valid;
}

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

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

Ok so now I am just still stumped on test 14

Welcome to the forum @twigginbarrys ,

Failing Test #14 points to an issue in your validateManifest() function.

Here are a couple of things to consider:

  • Shouldn’t you check the type of a property’s value before you test anything else for that value?
  • Should you manipulate a property’s value before you check its validity? (e.g manifest.unit.toLowerCase())

Happy coding!

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.