Build a Cargo Manifest Validator - Step 21, 25, 26

Issue:

My code is already working with the required output but somehow I can’t pass step 21, 25 and 26 which required same result. Kindly help me check my code, what did I do wrong.

const maniObject = {
   containerId: 68, 
   destination: "Salinas", 
   weight: 101, 
   unit: "lb", 
   hazmat: true };

function normalizeUnits(manifest){
  const copy = {...manifest};
  const outputcopy = {};
  if(copy.unit === "lb"){
    outputcopy.containerId = copy.containerId;
    outputcopy.destination = copy.destination;
    outputcopy.weight = copy.weight * 0.45;
    outputcopy.unit = "kg";
    outputcopy.hazmat = copy.hazmat;
    return outputcopy;
  };
  console.log()
return copy;
};

const invalidObj = { 
  containerId: 0, 
  destination: 405, 
  weight: -84, 
  unit: "pounds", 
  hazmat: "no" };

const valObject = { 
  containerId: 3, 
  destination: "asdf", 
  weight: 0, 
  unit: "kg", 
  hazmat: false } ;

function validateManifest(manifest){
  const copy = {...manifest};
  const errors = {};

  if (typeof copy !== "object") {
    errors.containerId = "Invalid";
    errors.destination = "Invalid";
    errors.weight = "Invalid";
    errors.unit = "Invalid";
    errors.hazmat = "Invalid";
  }
  
  if(!copy.hasOwnProperty("containerId") || copy.containerId === ""){
    errors.containerId = "Missing";
  }else if(
    copy.containerId === null ||
    copy.containerId <= 0 || 
    isNaN(copy.containerId) 
  ){ errors.containerId = "Invalid";
  }else if(!Number.isInteger(copy.containerId)){
    errors.containerId = "Invalid";
    errors.destination = "Missing";
    errors.weight = "Missing";
    errors.unit = "Missing"
     errors.hazmat = "Missing";
  };

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

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

  if(!copy.hasOwnProperty("unit") || copy.unit === ""){
    errors.unit = "Missing"
  }else if(copy.unit !== "kg" && copy.unit !== "lb" || typeof copy.unit !== "string"){
    errors.unit = "Invalid";
  };

  if(!copy.hasOwnProperty("hazmat") || copy.hazmat === ""){
    errors.hazmat = "Missing"
  }else if(typeof copy.hazmat !== "boolean"){
    errors.hazmat = "Invalid";
  };

return errors;
};

const processObj = { 
  containerId: 55, 
  destination: "Carmel", 
  weight: 400, 
  unit: "lb", 
  hazmat: false }


function processManifest(manifest){
const valid = validateManifest(manifest);

if(Object.keys(valid).length > 0){
    console.log(`Validation error: ${manifest.containerId}`);
    console.log(valid);
}else {
const normal = normalizeUnits(manifest);
console.log(`Validation success: ${normal.containerId}`);
console.log(`Total weight: ${normal.weight} ${normal.unit}`);
}

};

processManifest({ containerId: -88, destination: "Soledad", weight: NaN });


Hi @eugedev,

It’s probably confusing that the tests are pointing to processManifest as the problem, but I suggest looking at your validateManifest function instead.

For example, in validateManifest:

if(!copy.hasOwnProperty("containerId") || copy.containerId === ""){
    errors.containerId = "Missing";

If containerId is an empty string, does that mean the containerId property is missing?

And why do you set destination, weight, unit, and hazmat properties as “Missing” if the containerId property is not a number?

}else if(!Number.isInteger(copy.containerId)){
errors.containerId = “Invalid”;
errors.destination = “Missing”;
errors.weight = “Missing”;
errors.unit = “Missing”
errors.hazmat = “Missing”;
};

Happy coding!

Hi dhess,

Thank you for your reply, I will try to check my validateManifest code again.

i will try like this.

if(!copy.hasOwnProperty(“containerId”) || copy.containerId === undefined){
errors.containerId = “Missing”;

regarding,

else if(!Number.isInteger(copy.containerId)){
errors.containerId = “Invalid”;
errors.destination = “Missing”;
errors.weight = “Missing”;
errors.unit = “Missing”
errors.hazmat = “Missing”;
};

its because of step 12.

In Test #12, you are passing in an object with only the containerId property so, in that case, the other properties are missing. But if you pass in an object like this: {containerId: "88", destination: "Paris", weight: 5.5, unit: "lb", hazmat: false}, are all of those properties missing?

You only need one of those conditions to check whether a property is missing.

Hi @dhess ,

Thank you for your advise, I revise the whole validateManifest function based on your advise, I also realize that I have lots of repeated/unnecessary condition.

It’s working now :slight_smile:

removed by moderator

Congratulations on solving the challenge! You should be proud of your achievement…we are! But we are removing your working solution, so it is not available to others who have not yet done the work to get there. Again, congrats!

Noted and Thank you very much.