Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

  1. If the input manifest object is valid, your validateManifest function should return an empty object {}.
  2. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.

are not passing even though I have tried not allowing invalid inputs. i don’t know what is wrong.

Your code so far

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

  // CONTAINER ID
  if (
    !Object.hasOwn(manifest, "containerId") ||
    manifest.containerId === ""
  ) {
    errors.containerId = "Missing";
  } else if (
    !(
      (typeof manifest.containerId === "number" &&
        Number.isInteger(manifest.containerId) &&
        manifest.containerId > 0) ||

      (typeof manifest.containerId === "string" &&
        manifest.containerId.trim() !== "")
    )
  ) {
    errors.containerId = "Invalid";
  }

  // DESTINATION
  if (!Object.hasOwn(manifest, "destination")) {
    errors.destination = "Missing";
  } else if (
    typeof manifest.destination !== "string" ||
    manifest.destination.trim() === ""
  ) {
    errors.destination = "Invalid";
  }

  // WEIGHT
  if (!Object.hasOwn(manifest, "weight")) {
    errors.weight = "Missing";
  } else if (
    typeof manifest.weight !== "number" ||
    !Number.isInteger(manifest.weight) ||
    manifest.weight <= 0
  ) {
    errors.weight = "Invalid";
  }

  // UNIT
  if (!Object.hasOwn(manifest, "unit")) {
    errors.unit = "Missing";
  } else if (manifest.unit !== "kg") {
    errors.unit = "Invalid";
  }

  // HAZMAT
  if (!Object.hasOwn(manifest, "hazmat")) {
    errors.hazmat = "Missing";
  } else if (typeof manifest.hazmat !== "boolean") {
    errors.hazmat = "Invalid";
  }

  if(Object.keys(errors).length === 0){
      return {};
  };

  return errors;
}

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:150.0) Gecko/20100101 Firefox/150.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

Hi @yulebynn,

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

Happy coding!

Yes. If the input is an empty string it should hit the Missing. Is it not supposed to do that?

No. It is not missing; it is invalid then.

I’m afraid I’m not understanding you. If the input for containerId is empty string yes, it’s invalid and it will being taken care of by !Object.hasOwn(manifest, “containerId”) ||
manifest.containerId === “” as far as I’m seeing.

I discovered I’m using my outdated code. Here’s my new one but its still not passing these two:

  1. If the input manifest object is valid, your validateManifest function should return an empty object {}.
    17.If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
const manifestSample = {
  containerId:  "string", // allows string and number
  destination: "0", // allows string
  weight: "string", // valid number that is not a float or negative or 0
  unit:  "string", //only allows "kg"
  hazmat:  "string", //only allows
}const


function validateManifest(manifest){
  const errors = {};
  
  const id = manifest.containerId;
  const des = manifest.destination;
  const we = manifest.weight;
  
  // CONTAINER ID
  if (!Object.hasOwn(manifest, "containerId") || (typeof id === "string" && id.trim() === "")){
    errors.containerId = "Missing";
  }else if(id == undefined || id <= 0){
    errors.containerId = "Invalid";
  }else if(typeof id === "number" && !Number.isInteger(id)){
    errors.containerId = "Invalid";
  }

  // DESTINATION
  if(!manifest.destination){
    errors.destination = "Missing";
  }else if (typeof des === "string" && des.trim() === ""){
    errors.destination = "Invalid";
  }else if(typeof manifest.destination === "number"){
    errors.destination = "Invalid";
  };

  // WEIGHT
  if(manifest.weight <= 0 ||Number.isNaN(we) || typeof we === "string"){
    errors.weight = "Invalid";
  }else if(!manifest.weight || manifest.weight === "Undefined"){
    errors.weight = "Missing";
  };

  // UNIT
  if(!manifest.unit){
    errors.unit = "Missing";
  }else if (manifest.unit !== "kg"){
    errors.unit = "Invalid";
  };

  // HAZMAT
  if(manifest.hazmat === undefined){
      errors.hazmat = "Missing";
  }else if(typeof manifest.hazmat === "string" || typeof manifest.hazmat === "number" || manifest.hazmat === null){
    errors.hazmat = "Invalid";
  }
    
  if(Object.keys(errors).length === 0){
      return {};
  };

  return errors;
};
  

console.log(validateManifest(manifestSample));
console.log(typeof(manifestSample));

Each cargo manifest will be represented as an object with the following properties:

  • containerId: a positive integer identifying the associated cargo container.
  • destination: a non-empty string (after trimming whitespace) denoting the cargo’s target destination.
  • weight: a positive number representing the cargo’s weight.
  • unit: a string describing the units for the cargo’s weight property (either "kg" for kilograms or "lb" for pounds).
  • hazmat: a boolean value indicating whether hazardous material handling is needed.

Can containerId be either a string or a number? Is “kg” the only valid value for unit? Can hazmat be a string?

Please carefully review all of the instructions and user stories, then try again.

Tell us what’s happening:

I have no clue how to check whether the unit is “kg” OR “lb” only. I think this is not working else if(manifest.unit !== "kg" || manifest !== "lb")

Your code so far

const manifest1 = {
  containerId: 1, // numbers only
  destination: "santa cruz", // string only
  weight: 560, //numbers only
  unit: "lb", 
  hazmat: false, 
}

function validateManifest(manifest){
  const errors = {};
  
  // CONTAINER ID
  if(!Object.hasOwn(manifest, "containerId")){
    errors.containerId = "Missing";
  }else if(typeof manifest.containerId !== "number" || (typeof manifest.containerId === "string" &&manifest.containerId.trim() === "") || !manifest.containerId || manifest.containerId <= 0 || !Number.isInteger(manifest.containerId)){
    errors.containerId = "Invalid";
  };
  
  
  // DESTINATION
  if(!Object.hasOwn(manifest, "destination")){
    errors.destination = "Missing";
  }else if(typeof manifest.destination !== "string" || (typeof manifest.destination === "string" &&manifest.destination.trim() === "") || !manifest.destination){
    errors.destination = "Invalid";
  };

  // WEIGHT
  if(!Object.hasOwn(manifest, "weight")){
    errors.weight = "Missing";
  }else if(typeof manifest.weight !== "number" || (typeof manifest.weight === "string" && manifest.weight.trim() === "") || !manifest.weight || !Number.isInteger(manifest.weight) || manifest.weight <= 0){
    errors.weight = "Invalid";
  }; 

  // UNIT
  if(!Object.hasOwn(manifest, "unit")){
    errors.unit = "Missing";
  }else if(typeof manifest.unit !== "string" || (typeof manifest.unit === "string" && manifest.unit.trim() === "") || !manifest.unit || manifest.unit === "pounds"){
    errors.unit = "Invalid";
  }else if(manifest.unit !== "kg" || manifest !== "lb"){
    errors.unit = "Invalid";
  }; 

  // HAZMAN
  if(!Object.hasOwn(manifest, "hazmat")){
    errors.hazmat = "Missing";
  }else if(typeof manifest.hazmat !== "boolean" || (typeof manifest.hazmat === "string" && manifest.hazmat.trim() === "")){
    errors.hazmat = "Invalid";
  }; 

  if(Object.keys(errors).length === 0){
    return {};
  }

  return errors;
  
};


console.log(validateManifest(manifest1));
console.log(typeof(manifest1));

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:150.0) Gecko/20100101 Firefox/150.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

here you are checking the unit

what are you checking here?

if manifest.unit is not equal to “lb” or “kg”. Like I said I have no idea how to do that, that’s why I am asking here.

no, look well, are you checking the unit here?

here you are doing so, what’s different?

updated (manifest.unit !== “kg” || manifest.unit !== “lb”) is still counting as Invalid

The unit is still unit: "lb",

ok, now consider, what is the || doing? and if the unit is that, what will manifest.unit !== "kg" evaluate to?

The problem is the ||. Thanks about that!