Build a Cargo Manifest Validator - Build a Cargo Manifest Validator step 13 and 14

Tell us what’s happening:

I have tested my if statements for step 13 and 14 but they still are not passing and they still achieve the proper results. So I’m not sure what it is that I’ m doing wrong. My validate function passes each test besides 13 and 14.

Your code so far

const cargoManifest = {
  containerId: 1,
  destination: "Salias",
  weight: 101,
  unit: "lb",
  hazmat: true
}

const {containerId, destination, weight, unit, hazmat} = cargoManifest;

function normalizeUnits(manifest) {
  const copyManifest = {...manifest}
  
  if(copyManifest.unit === "lb") {
    copyManifest.weight = convertPoundsToKilo(copyManifest);
    copyManifest.unit = "kg";
  }
  return copyManifest;
}


function convertPoundsToKilo(obj) {
  let convert = obj.weight * 0.45;
  return convert;
}

function validateManifest(manifest) {
  const copyManifest = {...manifest};
  
  if(Object.keys(copyManifest).length === 0) {
    const missingManifest = {};
    missingManifest.containerId = "Missing";
    missingManifest.destination = "Missing";
    missingManifest.weight = "Missing";
    missingManifest.unit = "Missing";
    missingManifest.hazmat = "Missing";
    return missingManifest;

  };
  
  if(copyManifest.containerId === null) {
      const invalidContainer = {};
      invalidContainer.containerId = "Invalid";
      return invalidContainer;
    };
    
    if(containerId  < 0 || unit === "kg" || weight < 0 || Number.isInteger(copyManifest.destination)) {
      const invalidObj = {};
      invalidObj.containerId = "Invalid";
      invalidObj.destination = "Invalid";
      invalidObj.weight = "Invalid";
      invalidObj.unit = "Invalid";
      invalidObj.hazmat = "Invalid";
      return invalidObj;
    };
  
    if(!Number.isInteger(copyManifest.containerId) || copyManifest.containerId < 0) {
      const invalidValue = {};
      invalidValue.containerId = "Invalid";
      invalidValue.destination = "Missing";
      invalidValue.weight = "Missing";
      invalidValue.unit = "Missing";
      invalidValue.hazmat = "Missing";
      return invalidValue;

    };

    if(Number.isNaN(copyManifest.weight)) {
      const invalidWeight = {};
      invalidWeight.containerId = "Missing";
      invalidWeight.destination = "Missing";
      invalidWeight.weight = "Invalid";
      invalidWeight.unit = "Missing";
      invalidWeight.hazmat = "Missing";
      return invalidWeight;
    };

    if(!copyManifest.destination.trim()) {
      const destinationInvalid = {};
      destinationInvalid.containerId = "Missing";
      destinationInvalid.destination = "Invalid"; 
      destinationInvalid.weight = "Missing"; 
      destinationInvalid.unit = "Missing";
      destinationInvalid.hazmat = "Missing";
      return destinationInvalid;
    }
  return new Object();
}

console.log(validateManifest(cargoManifest));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

hello @SourceGlitch welcome back to the forum!

It looks like when your code is checking one property, it is also trying to guess and set the status of other properties as well.

Wouldn’t it be a better approach if each check took responsibility of only setting the status of the property it is currently checking? Something like this –

//containerId
CHECK status of containerId from manifest
  SET containerId status pnly

REPEAT for other properties

It seems like you are trying to ‘curate’ your validateManifest output to pass each test. Please try to fulfill the User Stories instead, so that your functions also pass the tests with inputs that are not necessarily present in the tests.

Hello, I think try only to change one key property in each statement cause in your function the Value of one key affects all the other keys.

I nested my functions for each key and went through the following levels

for each key ,

1.first checked if key was in object (if true - step2, else - missing)

2.then checking the type( if true - step 3, else -invalid)

3.for number type keys check positivity and Dp’s (where necessary) (if true - remove key , else - invalid) for string type keys check if they are true after trim () (true - remove key, else -assign appropriate value)

/*if(Number.isNaN(copyManifest.weight)) {
      const invalidWeight = {};
      invalidWeight.containerId = "Missing";
      invalidWeight.destination = "Missing";
      invalidWeight.weight = "Invalid";
      invalidWeight.unit = "Missing";
      invalidWeight.hazmat = "Missing";
      return invalidWeight;
    };

    if(!copyManifest.destination.trim()) {
      const destinationInvalid = {};
      destinationInvalid.containerId = "Missing";
      destinationInvalid.destination = "Invalid"; 
      destinationInvalid.weight = "Missing"; 
      destinationInvalid.unit = "Missing";
      destinationInvalid.hazmat = "Missing";
      return destinationInvalid;
    }
  return new Object();
}*/

Thank you for the breakdown this really help along side some more google searches and examples from other people post!!