Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I’m having issues passing tests 14 and 17-20. I think it might be an issue with the processManifest function, but I’m not sure. I tried making the if condition just the output of the validate function, but that causes other tests to break instead. I would appreciate some guidance.

Your code so far

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

  if(newMan.unit === "kg"){
    return newMan
  }
  else{
    newMan.weight = newMan.weight * 0.45;
    newMan.unit = "kg";

    return newMan;
  }
}

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

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

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

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

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

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

  return newMan;
}

function processManifest(manifest){
  if(validateManifest(manifest) === {}){
    console.log(`Validation success: ${manifest.containerId}`);
    console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`);
  }
  else{
    console.log(`Validation error: ${manifest.containerId}`);
    console.log(validateManifest(manifest));
  }
}

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

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

Welcome to the forum @seanmdunn17 ,

This is not a valid way to check for an empty object. Test it. Log validateManifest(manifest). Then log validateManifest(manifest) === {}.

Happy coding!

Alright, test 14 is still failing, but that resolved everything else. I was able to figure out how the check is supposed to look. Thanks so much for that!

removed by moderator for me this code has worked, I think it is a messy code but whatever works, right?

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.