Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Hello! I am stuck on test case 15 - If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
All the other test cases passed. Can someone point me in the right direction.

Your code so far

function normalizeUnits(manifest){
  let wt=manifest.weight;
  if (manifest['unit'] == 'lb'){
    wt = manifest.weight * 0.45;
  }

  const normalizedManifest = {
    containerId:manifest.containerId,
    destination:manifest.destination,
    weight:wt,
    unit:"kg",
    hazmat:manifest.hazmat
    }
  return normalizedManifest;
}

function validateManifest(manifest){
  const validated = {
    containerId:'Missing',
    destination:'Missing',
    weight:'Missing',
    unit:'Missing',
    hazmat:'Missing'
  };
  if (typeof manifest !== "object" || manifest === null || Array.isArray(manifest)){
    return validated;
  }

  if(manifest.hasOwnProperty('containerId')){
    validated.containerId='Invalid';
    if(manifest.containerId!==undefined && manifest.containerId!==null && Number.isInteger(manifest.containerId) && manifest.containerId>0 ){
      delete validated.containerId;
    }
  }
    if(manifest.hasOwnProperty('destination')){
    validated.destination='Invalid';
    if(manifest.destination!==undefined && manifest.destination!==null && typeof manifest.destination === 'string' && manifest.destination.trim()!==""){
      delete validated.destination;
    }
  }
    if(manifest.hasOwnProperty('weight')){
    validated.weight='Invalid';
    if(manifest.weight!==undefined && manifest.weight!==null && Number.isInteger(manifest.weight) && manifest.weight>0){
      delete validated.weight;
    }
  }
    if(manifest.hasOwnProperty('unit')){
    validated.unit='Invalid';
    if(manifest.unit!==undefined && manifest.unit!==null && typeof manifest.unit==='string' && (manifest.unit.toLowerCase() =='kg' || manifest.unit.toLowerCase() =='lb')){
      delete validated.unit;
    }
  }
    if(manifest.hasOwnProperty('hazmat')){
    validated.hazmat='Invalid';
    if(manifest.hazmat!==undefined && manifest.hazmat!==null && (manifest.hazmat==true || manifest.hazmat==false)){
      delete validated.hazmat;
    }
  }
  return validated;
}

function processManifest(manifest){
  if(Object.keys(validateManifest(manifest)).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 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0

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 @MekhAC,

Should you be manipulating a value before you validate it?

Can hazmat be 0?

Happy coding

manifest.hazmat==true || manifest.hazmat==false here, the equality operator is loosely typed, it allows other non-boolean values, the evaluation needs to strictly check if the data type itself is a boolean.

Also here Number.isInteger(manifest.weight) This logic specifically checks if the weight is an integer. However, real-world shipping weights often include decimals, and the test likely passes valid decimal weights to ensure the code can handle them. Because the integer check automatically rejects any number with a fraction, it incorrectly flags perfectly valid decimal weights as invalid

in the units validation, using

` if(manifest.unit!==undefined && manifest.unit!==null && typeof manifest.unit===‘string’ && (manifest.unit.toLowerCase() ==‘kg’ || manifest.unit.toLowerCase() ==‘lb’)) `

should work right? Since datatype is checked to be string

Adding boolean typechecking for hazmat doesn’t seem to work either

if(manifest.hazmat!==undefined && manifest.hazmat!==null && typeof manifest.hazmat === 'boolean')

Typechecking hazmat for boolean and typechecking weight for numbers doesn’t work either. I also didn’t us Number.isNaN() which was mentioned in the test cases, which I added now

if(manifest.weight!==undefined && manifest.weight!==null && typeof manifest.weight===‘number’ && !Number.isNaN(manifest.weight) && manifest.weight>0)

if(manifest.hazmat!==undefined && manifest.hazmat!==null && typeof manifest.hazmat === ‘boolean’)

What if the unit is "KG’? Should that validate?

This should work.

Happy coding

Oh my! You are right, the instructions explicitly said only ‘kg’ and ‘lb’ were to be accepted. Thank you, it finally worked!