Build a Cargo Manifest Validator - Test 7

Tell us what’s happening:

I’m having trouble with Test 7: If the input manifest object is valid, your validateManifest function should return an empty object {}. I’ve logged the function with valid arguments, and the console displays {}. Every other test passes.

Your code so far

const normalizeUnits = (manifest) => {
  if (manifest.unit !== "lb") return { ...manifest };

  return {
    ...manifest,
    weight: manifest.weight * 0.45,
    unit: "kg"
  };
};

const validateManifest = (manifest) => {
  const errors = {};

  if (!("containerId" in manifest)) {
    errors.containerId = "Missing";
  } else if (!Number.isInteger(manifest.containerId) || manifest.containerId <= 0) { 
    errors.containerId = "Invalid";
  }

  if (!("destination" in manifest)) {
    errors.destination = "Missing";
  } else if (typeof manifest.destination !== "string" || manifest.destination.trim() === "") {
    errors.destination = "Invalid";
  }

  if (!("hazmat" in manifest)) {
    errors.hazmat = "Missing";
  } else if (typeof manifest.hazmat !== "boolean") {
    errors.hazmat = "Invalid";
  }

  if (!("weight" in manifest)) {
    errors.weight = "Missing";
  } else if (typeof manifest.weight !== "number" || manifest.weight <= 0 || isNaN(manifest.weight)) {
    errors.weight = "Invalid";
  }

  if (!("unit" in manifest)) {
    errors.unit = "Missing";
  } else if (manifest.unit !== "kg") {
    errors.unit = "Invalid";
  }

  return errors;
};

const processManifest = (manifest) => {

  const fixedWeight = normalizeUnits(manifest);
  const errors = validateManifest(fixedWeight);

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

Your browser information:

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

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

hello @dataylor915 welcome to the forum!

Does your code validate unit: "lb"?

Ah, I understand. If the manifest has “lb”, the manifest would be valid. Therefore, the function should normalize it to “kg” in that case. Thank you.