Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

My code does what is required but I can’t seem to get test 16 to succeed?

I am a newbie to this whole coding thing. I fully understand the problem lies with my over complicated code… I just don’t know how to fix.

Your code so far

function normalizeUnits(manifest) {

if (manifest.unit === "lb") {
  let newManifest = {...manifest};
  newManifest.unit = "kg";
  newManifest.weight = newManifest.weight * 0.45;
  return newManifest;}

else {
  let newManifest = {...manifest};
  return newManifest;
}
}

function validateManifest(manifest) {


let invalidManifest = {};
if (manifest.containerId > 0 && typeof manifest.destination === "string" && manifest.weight > 0 && manifest.unit === "kg" || manifest.unit === "lb" && typeof manifest.hazmat === "boolean") {
  let validManifest = {};
  return validManifest;
  } 
   
else {

if (!manifest.containerId) {
        invalidManifest.containerId = "Missing";
      }

if (manifest.containerId <= 0 || manifest.containerId === null || manifest.containerId && manifest.containerId != Number.isInteger()) {
        invalidManifest.containerId = "Invalid";
      }

if (!manifest.destination) {
        invalidManifest.destination = "Missing";
      }

if (manifest.destination && typeof manifest.destination !== "string" || manifest.destination && manifest.destination.trim() === "") {
        invalidManifest.destination = "Invalid";
}

if (!manifest.weight) {
  invalidManifest.weight = "Missing";
}

if (manifest.weight <= 0 || Number.isNaN(manifest.weight)) {
  invalidManifest.weight = "Invalid";
}

if (manifest.unit === null || !manifest.unit) {
  invalidManifest.unit = "Missing";
}

if (typeof manifest.unit === "number" || typeof manifest.unit === "string" && manifest.unit != "kg" && manifest.unit != "lb") {
  invalidManifest.unit = "Invalid";
}

if (manifest.hazmat === null || manifest.hazmat === undefined) {
  invalidManifest.hazmat = "Missing";
}

if (manifest.hazmat && typeof manifest.hazmat !== "boolean") {
  invalidManifest.hazmat = "Invalid";
}

return invalidManifest
}
}





function processManifest(manifest) {

if (manifest.containerId > 0 && typeof manifest.destination === "string" && manifest.weight > 0 && manifest.unit === "kg" || manifest.unit === "lb" && typeof manifest.hazmat === "boolean") {

    let newManifest = normalizeUnits(manifest);

    let containerId = newManifest.containerId;  
    let weight = newManifest.weight;

    console.log(`Validation success: ${containerId}`); 
    console.log(`Total weight: ${weight} kg`);
  }

  else {
    let containerId = manifest.containerId;  
    
    console.log(`Validation error: ${containerId}`); 
    console.log(validateManifest(manifest));
  }
}
let manifest = {
   containerId: 55,
   destination: "Santa Cruz", 
   weight: 304, 
   unit: "kg", 
   hazmat: false 
}
console.log(normalizeUnits(manifest));
console.log(validateManifest(manifest));
console.log(processManifest(manifest));

Your browser information:

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

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

are you sure that a falsy value always mean Missing? consider containerId: false, it’s present but invalid

you should not try to catch all values that give Missing in the next line, you should check for the present or absence of the property, and this is so for all the properties

are you sure that null means it’s missing?

are you sure that there aren’t other invalid values?

Welcome to the forum @alastair.kingon,

Is a unit’s value invalid if its type is number or string? What should its type be? How can you say, “is not this type”?

If the value of unit or hazmat is null, does that mean the key is missing?

Also, you may want to organize your code so that you do not do any checks for validity if the key is missing.

Happy coding