Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

This code fails test 15 even though the output displays everything correctly. Can you tell me why this code fails test 15? What should I fix to make the code pass test 15?

Your code so far

function normalizeUnits (manifest) {
  const newManifest = {...manifest};

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

  return newManifest;
}

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

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

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

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

  if (manifest.unit === undefined) {
    errors.unit = "Missing";
  } else if (typeof manifest.unit !== "string" || !["kg", "lb"].includes(manifest.unit.toLowerCase())) {
    errors.unit = "Invalid";
  }

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

  return errors;
}

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(validateManifest(manifest));
  }
}

const cargo1 = {
  containerId: 68,
  destination: "Salinas",
  weight: 101,
  unit: "lb",
  hazmat: true
};

console.log(normalizeUnits(cargo1));

const cargo2 = {
  containerId: 1,
  destination: "Santa Cruz",
  weight: 304,
  unit: "kg",
  hazmat: false
};

console.log(validateManifest(cargo2));
console.log(validateManifest({}));

const cargo3 = {
  containerId: null,
  destination: "Santa Cruz",
  weight: 304,
  unit: "kg",
  hazmat: false
};

console.log(validateManifest(cargo3));

const cargo4 = {
  containerId: 0,
  destination: 405,
  weight: -84,
  unit: "pounds",
  hazmat: "no"
};

console.log(validateManifest(cargo4));

const cargo5 = {
  containerId: -2
};

console.log(validateManifest(cargo5));

const cargo6 = {
  containerId: 3.50
};

console.log(validateManifest(cargo6));

const cargo7 = {
  destination: "  "
};

console.log(validateManifest(cargo7));

const cargo8 = {
  weight: NaN
};

console.log(validateManifest(cargo8));

const cargo9 = {
  containerId: 55,
  destination: "Carmel",
  weight: 400,
  unit: "lb",
  hazmat: false
};

processManifest(cargo9);

const cargo10 = {
  containerId: -88,
  destination: "Soledad",
  weight: NaN
};

processManifest(cargo10);

const cargo11 = {
  destination: "Watsonville",
  hazmat: true
};

processManifest(cargo11);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

GitHub Link: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md

Welcome to the forum @lkhoa8606,

Does it make sense to manipulate this value before you test its validity?

Happy coding