Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

test 15 failed, although my function returns a object with the describing missing and/or invalid properties.

Your code so far

let cargo = {
  containerId: 1,
  destination: "Salinas",
  weight: 10,
  unit: "kg",
  hazmat: true
}

const normalizeUnits = (manifest) => {
  const isKg = manifest.unit.toLowerCase() === "kg";

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

let 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;
}

const 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);
  }
}

processManifest(cargo)

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

Welcome to the forum @khode_geek

let cargo = {
  destination: "Salinas",
  weight: 10,
  unit: "kg",
  hazmat: true
}

Validation error: undefined
{ containerId: 'Missing' }

When I removed the containerId key/value pair from the cargo object, I got an undefined.

Happy coding

Hi @khode_geek,

Is zero a positive number?

Should you manipulate the unit value before checking its validity?

Happy coding!