Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I am stuck with the test case 15. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
Any help will be appreciated.

Your code so far

let manifest = { destination: "Watsonville", hazmat: true }

function normalizeUnits(manifest){
  let updatedManifest = {...manifest};
  if(updatedManifest["unit"] == "lb"){
    updatedManifest["weight"] = updatedManifest["weight"]*0.45;
    updatedManifest["unit"] = "kg";
  }
  return updatedManifest;
}
function validateManifest(manifest){
  let validatedManifest = {};
  if(("containerId" in manifest) == false){
    validatedManifest["containerId"] = "Missing";
  }else if(!Number.isInteger(manifest["containerId"]) || manifest["containerId"] == null || manifest["containerId"] == undefined || manifest["containerId"] <1){
    validatedManifest["containerId"] = "Invalid"
  }
  if(("destination" in manifest) == false){
    validatedManifest["destination"] = "Missing";
  }else if(typeof manifest["destination"] != "string"){
    validatedManifest["destination"] = "Invalid";
  }else if(manifest["destination"].trim().length == 0){
    validatedManifest["destination"] = "Invalid";
  }
  if(("weight" in manifest) == false){
    validatedManifest["weight"] = "Missing";
  }else if(typeof manifest["weight"] !== "number" || Number.isNaN(manifest["weight"]) || manifest["weight"]<0){
    validatedManifest["weight"] = "Invalid";
  }
  if(("unit" in manifest) == false){
    validatedManifest["unit"] = "Missing";
  }else if(manifest["unit"] != "kg" && manifest["unit"] != "lb"){
    validatedManifest["unit"] = "Invalid";
  }
  if(("hazmat" in manifest) == false){
    validatedManifest["hazmat"] = "Missing";
  }else if(typeof manifest["hazmat"] != "boolean"){
    validatedManifest["hazmat"] = "Invalid";
  }
  return validatedManifest;
}
function isEmptyObject(obj){
  return obj !== null && 
         typeof obj === 'object' && 
         !Array.isArray(obj) && 
         Object.keys(obj).length === 0;
}
function processManifest(manifest){
  if(isEmptyObject(validateManifest(manifest))){
    console.log(`Validation success: ${manifest["containerId"]}`);
    console.log(`Total weight: ${normalizeUnits(manifest)["weight"]} kg`);
  }else{
    console.log(`Validation error: ${manifest["containerId"]}`);
    console.log(validateManifest(manifest));
  }
}

console.log(validateManifest(manifest));
processManifest(manifest);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.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

Welcome to the forum @Kinkar,

Here’s an excerpt from the instructions:

  • weight: a positive number representing the cargo’s weight.

Is zero a positive number?

Happy coding

test your code with this

console.log(validateManifest(  {
    containerId: -6,
    destination: 123,
    weight: 0,
    unit: "pounds",
    hazmat: true
  }))

what should it return? using your reasoning and the given rules, what is it supposed to return?

what is your function returning?

only after reflecting, click here to see the expected output
{
    "containerId": "Invalid",
    "destination": "Invalid",
    "weight": "Invalid",
    "unit": "Invalid"
}

This helped. Thanks.