Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Test 16 won’t pass. I believe my if else chains satisfy all conditions. But, I’m not getting why 16 doesn’t pass the checks.

Your code so far


const manifest = {};

function normalizeUnits(manifest) {
  const correction = { ...manifest };
  if (typeof manifest.unit === "string" && manifest.unit.toLowerCase() === "lb") {
    correction.weight = Number((manifest.weight * 0.45).toFixed(2));
    correction.unit = "kg"; 
  }
  return correction;
}

function validateManifest(manifest) {
  const issues = {};

  if (
    manifest === null ||
    typeof manifest !== "object" ||
    Array.isArray(manifest)
  ) {
    return { manifest: "Invalid" };
  }

  if (!Object.prototype.hasOwnProperty.call(manifest, "containerId")) {
    issues.containerId = "Missing";
  } else if (
    typeof manifest.containerId !== "number" ||
    !Number.isInteger(manifest.containerId) ||
    manifest.containerId <= 0
  ) {
    issues.containerId = "Invalid";
  }

  if (!Object.prototype.hasOwnProperty.call(manifest, "destination")) {
    issues.destination = "Missing";
  } else if (
    typeof manifest.destination !== "string" ||
    manifest.destination.trim() === ""
  ) {
    issues.destination = "Invalid";
  }

  if (!Object.prototype.hasOwnProperty.call(manifest, "weight")) {
    issues.weight = "Missing";
  } else if (
    typeof manifest.weight !== "number" ||
    !Number.isFinite(manifest.weight) ||
    manifest.weight <= 0
  ) {
    issues.weight = "Invalid";
  }

  if (!Object.prototype.hasOwnProperty.call(manifest, "unit")) {
    issues.unit = "Missing";
  } else if (
    typeof manifest.unit !== "string" ||
    !["lb", "kg"].includes(manifest.unit.toLowerCase())
  ) {
    issues.unit = "Invalid";
  }

  if (!Object.prototype.hasOwnProperty.call(manifest, "hazmat")) {
    issues.hazmat = "Missing";
  } else if (typeof manifest.hazmat !== "boolean") {
    issues.hazmat = "Invalid";
  }

  return issues;
}


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

if (typeof module !== "undefined") {
  module.exports = { normalizeUnits, validateManifest, processManifest };
}

if (typeof require === "undefined" || require.main === module) {
  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/152.0.0.0 Safari/537.36 OPR/136.0.0.0

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @ultra-onyx,

If you run the tests then open up your browser’s console, you’ll see this assertion error:

image

That should give you an idea about where you should look to debug your code.

Happy coding