Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I’m having trouble passing instruction 14. Could anyone give me a hint? I’d really appreciate it :blush:
14. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.

Your code so far

let cargo1 = {
 containerId: 0,
  destination: 55,
  unit: "lb ",
  hazmat: "sc",
}

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

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

  return newManifest;
};

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



processManifest(cargo1)

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

**Okay, it worked. **

removed by moderator

I think it passed because function declarations are fully hoisted, while arrow functions assigned to const are not available before initialization.

Congratulations on solving the challenge! You should be proud of your achievement…we are! But we are removing your working solution, so it is not available to others who have not yet done the work to get there. Again, congrats!

1 Like

please how did you get it to work

Welcome to the forum @sneptune!

Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Help button image located on each challenge. This will automatically import your code in a readable format and pull in the challenge URL while still allowing you to ask any question about the challenge or your code.

Thank you.

1 Like