Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I keep getting the following errors:
8. If the input manifest object is valid, your validateManifest function should return an empty object {}.
22. If the input manifest object is valid, your processManifest function should log a success message with the object’s containerId, and then log the object’s weight in kilograms. You should use normalizeUnits() for the conversion and have two console.log() calls.

I have been working on this for a couple of days, so any help is much appreciated!

Your code so far

function normalizeUnits (manifest) {
  let newManifest = {...manifest};
  if (newManifest.unit === "lb") {
    newManifest.weight = newManifest.weight * 0.45;
    newManifest.unit = "kg";
    return newManifest;
  };
  return newManifest;
}


function validateManifest (manifest) { 
  let normalizedManifest = normalizeUnits(manifest);
  let nextManifest = {}; 
  let invalid = false;
  
  if (!Object.hasOwn(manifest, "containerId")) {
    nextManifest.containerId = "Missing";
    invalid = true;

  } else if (manifest.containerId < 1 || Number.isInteger(manifest.containerId) === false || manifest.containerId === null) {
    nextManifest.containerId = "Invalid";
    invalid = true;
  }

  if (!Object.hasOwn(manifest, "destination")) {
    nextManifest.destination = "Missing";
    invalid = true;

  } else if (typeof manifest.destination !== "string" || manifest.destination.trim() === "") {
    nextManifest.destination = "Invalid";
    invalid = true;
  }

  if (!Object.hasOwn(normalizedManifest, "weight")) {
    nextManifest.weight = "Missing";
    invalid = true;

  } else if (Number.isInteger(normalizedManifest.weight) === false || manifest.weight < 1 || Number.isNaN(normalizedManifest.weight)) {
    nextManifest.weight = "Invalid";
    invalid = true;
  }

  if (!Object.hasOwn(normalizedManifest, "unit")) {
    nextManifest.unit = "Missing";
    invalid = true;

  } else if (normalizedManifest.unit !== "kg") {
    nextManifest.unit = "Invalid";
    invalid = true;
  }

  if (!Object.hasOwn(manifest, "hazmat")) {
    nextManifest.hazmat = "Missing";
    invalid = true;

  } else if (typeof manifest.hazmat !== "boolean") {
    nextManifest.hazmat = "Invalid";
    invalid = true;
  }
  
  if (invalid) {
    return nextManifest;
  } else {
    return {}
  }
}


function processManifest (manifest) {
  let nextManifest = validateManifest(manifest);
  let newManifest = normalizeUnits(manifest);

  if (Object.hasOwn(nextManifest, "containerId") || Object.hasOwn(nextManifest, "destination") || Object.hasOwn(nextManifest, "weight") || Object.hasOwn(nextManifest, "unit") || Object.hasOwn(nextManifest, "hazmat")) {
    console.log(`Validation error: ${manifest.containerId}`);
    return console.log(nextManifest);
  } else {
    console.log(`Validation success: ${newManifest.containerId}`);
    console.log(`Total weight: ${newManifest.weight} kg`);
  }
}

processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false }) 

Your browser information:

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

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @ProllyBrundon,

Does it make sense to manipulate the values before you validate them?

Happy coding