Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Hello, I am unable to pass test 9 (which i think consequently means i can’t pass 15) - all else are passing, I’m not sure where i’m going wrong!

Your code so far


//a function which takes manifest as a paramater//must not mutate orignial object
    //create a copy of the original object to change instead 
const normalizeUnits = (manifest) => {
  let cloneManifest = Object.assign({}, manifest);
  if (cloneManifest.unit === "lb"){
    cloneManifest.unit ="kg";
    cloneManifest.weight = manifest.weight * 0.45;}

  return cloneManifest
  }
      // if the unit is in lb, change to kg AND *0.45
      //else unchanged
    

//




const validateManifest = (manifest) => {

  let cloneManifest = Object.assign({}, manifest);

  //containerID
  if(cloneManifest.containerId == null ){
    cloneManifest.containerId = "Missing"
  }
  else if(!Number.isInteger(manifest.containerId) || manifest.containerId <= 0)
  {cloneManifest.containerId = "Invalid"}
  else{
    delete cloneManifest.containerId;
  }
 
//destination
  if(cloneManifest.destination == null ){
    cloneManifest.destination ="Missing";
  }
  else if (typeof cloneManifest.destination !== "string" || (cloneManifest.destination.trim().length === 0)) {
  cloneManifest.destination = "Invalid" }

  else{
    delete cloneManifest.destination}
  
//weight
    if(cloneManifest.weight == null ){
      cloneManifest.weight = "Missing";
    }
    else if (isNaN(cloneManifest.weight) ||cloneManifest.weight < 0){
      cloneManifest.weight = "Invalid"
    } 
    else{
      delete cloneManifest.weight
    }

   //unit   
      if (cloneManifest.unit == null){cloneManifest.unit = "Missing"}
      else if (cloneManifest.unit !== "kg" && cloneManifest.unit!== "lb"){
        cloneManifest.unit = "Invalid"
      }
      else{delete cloneManifest.unit}
     
  //hazmat
if (cloneManifest.hazmat == null ){
  cloneManifest.hazmat = "Missing";
}
else if(typeof cloneManifest.hazmat !== "boolean"){
  cloneManifest.hazmat = "Invalid"
}
else {delete cloneManifest.hazmat};


return cloneManifest;
}
//process manifest

const processManifest = (manifest) =>{
  const normalizedManifest = normalizeUnits(manifest);
   //if the manifest object is valid "Validation success: ${containerId}"
  if (Object.keys(validateManifest(manifest)).length ==0) {
    console.log(`Validation success: ${manifest.containerId}`);
 //then the manifest's weight in kg Total weight: ${weight} kg. Use normalizeUnits() 
console.log(`Total weight: ${normalizedManifest.weight} kg`) 
  }

//if the manifest object is not validValidation error: ${containerId}
    //then the object returned
else{
  const validatedManifest = validateManifest(manifest);
  console.log(`Validation error: ${manifest.containerId}`);
  console.log(validatedManifest);
  
}

}
 

  

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

hello!

when a manifest has containerId: null, then validateManifest should mark it as "Invalid”. Your’s currently marks it as "Missing”.

if a property is not present at all, are you sure you should be checking for null here?

Hi, thanks, this is where I am stuck, if i put i put cloneManifest.containerId == undefined it doesn’t work at all

I’ve worked it out now! thank you so much you really pointed me in the right direction!