Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Test 16 has failed, and it is not possible to reproduce the error without the argument.

Your code so far

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

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

    return newManifest;
  }
}

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

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

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

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

  if(manifest.hasOwnProperty("unit")){
    if(typeof manifest.unit !== "string" || ((manifest.unit.toLowerCase() != "kg") && (manifest.unit.toLowerCase() != "lb"))){
      newManifest.unit = "Invalid";
    }
  } else{
    newManifest.unit = "Missing";
  }
  
  if(manifest.hasOwnProperty("hazmat")){
    if(typeof manifest.hazmat !== "boolean"){
      newManifest.hazmat = "Invalid";
    }
  } else{
    newManifest.hazmat = "Missing";
  }

  return newManifest;
}

function processManifest(manifest){

  const valid = validateManifest(manifest);

  if(Object.keys(valid).length === 0){
    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));
  }
}


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.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

are you sure that kG Kg KG lB Lb LB are valid?

I suppose you’re referring to the fact that I used manifest.unit.toLowerCase() != "kg" and manifest.unit.toLowerCase() != "lb" method. But that was just an experiment. Even without that method, I still have the same problem.

well, is 0 a valid weight?

are you sure that 0 is a positive number? isn’t the definition of positive to be above 0?

also, would not it be really weird to have a shipment of weight 0? what is being shipped there, air?

Thanks! That was the solution.