Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I got stuck at test 9, please check my code for a possible solution. All other test met the requirements except for 9.

/**
 * 1-4: Normalizes units from lb to kg (1 lb = 0.45 kg)
 */
function normalizeUnits(manifest) {
  const newManifest = { ...manifest };

  if (newManifest.unit === "lb") {
    newManifest.weight = parseFloat((newManifest.weight * 0.45).toFixed(2));
    newManifest.unit = "kg";
  }

  return newManifest;
}

/**
 * 5-16: Validates the manifest object
 */
function valid

Your code so far

/**
 * 1-4: Normalizes units from lb to kg (1 lb = 0.45 kg)
 */
function normalizeUnits(manifest) {
  const newManifest = { ...manifest };

  if (newManifest.unit === "lb") {
    newManifest.weight = parseFloat((newManifest.weight * 0.45).toFixed(2));
    newManifest.unit = "kg";
  }

  return newManifest;
}

/**
 * 5-16: Validates the manifest object
 */
function validateManifest(manifest) {
  const result = {};
  const fields = ["containerId", "destination", "weight", "unit", "hazmat"];

  fields.forEach((field) => {
    if (!(field in manifest) || manifest[field] === null || manifest[field] === undefined) {
      result[field] = "Missing";
    }
  });

  if (manifest.containerId !== undefined && manifest.containerId !== null) {
    if (!Number.isInteger(manifest.containerId) || manifest.containerId <= 0) {
      result.containerId = "Invalid";
    }
  }

  if (manifest.destination !== undefined && manifest.destination !== null) {
    if (typeof manifest.destination !== "string" || manifest.destination.trim() === "") {
      result.destination = "Invalid";
    }
  }

  if (manifest.weight !== undefined && manifest.weight !== null) {
    if (typeof manifest.weight !== "number" || Number.isNaN(manifest.weight) || manifest.weight <= 0) {
      result.weight = "Invalid";
    }
  }

  if (manifest.unit !== undefined && manifest.unit !== null) {
    if (manifest.unit !== "kg" && manifest.unit !== "lb") {
      result.unit = "Invalid";
    }
  }

  if (manifest.hazmat !== undefined && manifest.hazmat !== null) {
    if (typeof manifest.hazmat !== "boolean") {
      result.hazmat = "Invalid";
    }
  }

  return result;
}

/**
 * 17-26: Processes manifest, validates, and logs results
 */
function processManifest(manifest) {
  const errors = validateManifest(manifest);

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

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

Hi @anthonybassey ,

If a property’s value is null, does that mean the property is missing?

Happy coding!

I have tried the code you sent above, it’s been existing already in 5-16 second paragraph, if you check, I tried to use it when I crossed check it’ there the validateManifest for requirement 9 is still popping up for appropriate code to get the test pass. I will appreciate if can help resolved. Thanks.

Yes. That is your code, which I copied to show you where you should look in your code to find the issue. And the question I asked

was intended to help you understand why there is an issue with that code.

For three days I have been on that, will try to figure out. Thank you.

You will. Just think carefully about the question I asked. Good luck!

1 Like