Build a Cargo Manifest Validator

Hi All!

Currently stuck at: 14. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.

Any hint would be great!

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

function validateManifest(manifest){
  let newManifest = {};
  let broken = false;
  if(!Object.hasOwn(manifest, "containerId")){
    newManifest.containerId = "Missing";
    broken = true;
  } else if(manifest.containerId < 1 || Number.isInteger(manifest.containerId) == false || typeof manifest.containerId !== "number"){
    newManifest.containerId = "Invalid";
    broken = true;
  }

  if(!Object.hasOwn(manifest, "destination")){
    newManifest.destination = "Missing";
    broken = true; 
  } else if(typeof manifest.destination !== "string" || manifest.destination.trim() === ""){
    newManifest.destination = "Invalid";
    broken = true;
  } 

  if(!Object.hasOwn(manifest, "weight")){
    newManifest.weight = "Missing";
    broken = true; 
  } else if(manifest.weight < 0 || typeof manifest.weight !== "number" || Number.isNaN(manifest.weight) == true ){
    newManifest.weight = "Invalid";
    broken = true;
  }

  if(!Object.hasOwn(manifest, "unit")){
    newManifest.unit = "Missing";
    broken = true; 
  } else if(manifest.unit != "lb" && manifest.unit != "kg"){
    newManifest.unit = "Invalid";
    broken = true;
  }

  if(!Object.hasOwn(manifest, "hazmat")){
    newManifest.hazmat = "Missing";
    broken = true; 
  } else if(typeof manifest.hazmat !== "boolean"){
    newManifest.hazmat = "Invalid";
    broken = true;
  }   

  if (broken == true){
    return newManifest;
  } else{
    newManifest = Object();
    return newManifest;
  }
}

Thank!

Hi @Ouwesokken ,

Please post a link to this challenge.


In the future, if you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Hi @Ouwesokken,

I can already see one required part missing:

  1. You should implement a function named processManifest with a manifest parameter. The function should log:
  • If the manifest object is valid, Validation success: ${containerId} and then the manifest’s weight in kilograms as such, Total weight: ${weight} kg. Use normalizeUnits() for this conversion.
  • If the manifest object is not valid, Validation error: ${containerId} and then the object returned by calling validateManifest() with the manifest object.

Right now, I only see normalizeUnits() and validateManifest(), but I do not see a processManifest() function yet.

Also, a couple of other things to review:

  • In your weight validation, you used manifest.weight < 0. If the instructions expect the weight to be a positive number, then 0 should also be invalid!
  • In processManifest(), make sure you log exactly what the instructions ask for, including the exact text format:
    • Validation success: ${containerId}
    • Total weight: ${weight} kg
    • or, if invalid:
      • Validation error: ${containerId}
      • then the object returned by validateManifest(manifest)

So I’d suggest:

  1. Add the missing processManifest(manifest) function first.
  2. Double-check whether weight should reject 0 as well.
  3. Make sure your console.log() output matches the required text exactly.

Hope that helps.

Thanks! will use that button next time

Hi Yasser-diab,

Thanks for the hint as the problem was with the weight condition. :+1:

1 Like

You’re welcome!
Enjoy coding :blush: