Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I don’t think I understand the gyst of the project. Can you help guide me as to how I can approach this?

Your code so far

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

function validateManifest(manifest) {
  let results = {}
  if(manifest === null || manifest === undefined) {
    return "Missing", "Invalid"
  }
  if(manifest !== null || manifest !== undefined) {
    return results
  }
}

function processManifest(manifest) {
  if(validateManifest(manifest)) {
    console.log(`Validation success: ${containerId}`, `Total weight: ${weight} kg`)
    
  if(!validateManifest(manifest)) {
    console.log(`Validation error: ${containerId}`, validateManifest(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

You should start by writing valid code for each user story.
For eg. the first user story talks about writing a function called normalizeUnits.
You can test it by calling it like this:

console.log(normalizeUnits({
  containerId: 1,
  destination: "Monterey, California, USA",
  weight: 831,
  unit: "lb",
  hazmat: false
}))

In your case, your function doesn’t work. So read through again and if you have a specific question about something you are reading, let us know.

Ok, so I reformatted my code and made some edits, and the normalizeUnits() function turned out successful. However, I am currently stuck in the validateManifest() function. I do not know how the function should return an empty object if the function has to compare all the object’s properties for validation first. As you can see, I made multiple if, else if conditions to try to satisfy the conditions, but somehow they do not turn out correct.

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

///validateManifest()
function validateManifest(manifest) {
  let manifestCopy = {...manifest}
  if(Object.hasOwn(manifestCopy, containerId) && manifestCopy.containerId >= 1) {
   return Object()
}
  if(!Object.hasOwn(manifestCopy, containerId)){
   manifestCopy.containerId = "Missing"
} 

  if (typeof manifestCopy.containerId !== "number" || manifestCopy.containerId < 1) {
   manifestCopy.containerId = "Invalid"
}

  if(Object.hasOwn(manifestCopy, "destination") && typeof manifestCopy.destination === "string") {
   return Object()
}
  if(!Object.hasOwn(manifestCopy, "destination")){
   manifestCopy.destination = "Missing"
} 

  if (typeof manifestCopy.destination !== "string") {
   manifestCopy.destination = "Invalid"
}

  if(Object.hasOwn(manifestCopy, "weight") && typeof manifestCopy.weight === "number" && manifestCopy.weight >= 1) {
   return Object()
}
  if(!Object.hasOwn(manifestCopy, "weight")){
   manifestCopy.weight = "Missing"
} 

  if (typeof manifestCopy.weight !== "number" || manifestCopy.weight < 1) {
   manifestCopy.weight = "Invalid"
}

if(Object.hasOwn(manifest, "unit") && typeof manifestCopy.unit === "string" && manifestCopy.unit === "lb" || manifestCopy.unit === "kg") {
   return Object()
}
  if(!Object.hasOwn(manifest, "unit")){
   manifest.containerId = "Missing"
} 

  if (typeof manifestCopy.unit !== "string" || manifestCopy.unit !== "lb" || manifestCopy.unit !== "kg") {
   manifestCopy.unit = "Invalid"
}


if(Object.hasOwn(manifestCopy, "hazmat") && typeof manifestCopy.hazmat === "boolean" && manifestCopy.hazmat === true || manifestCopy.hazmat === false  ) {
   return Object()
}
  if(!Object.hasOwn(manifestCopy, "hazmat")){
   manifestCopy.hazmat = "Missing"
} 

  if (typeof manifestCopy.hazmat !== "boolean" || manifestCopy.hazmat === true || manifestCopy.hazmat === false) {
   manifestCopy.hazmat = "Invalid"
}

  return manifestCopy
}

///processManifest()

function processManifest(manifest) {
  if(validateManifest() === {}) {
    
    console.log(`Validation success: ${containerId}`) 
    const updatedWeight = normalizeUnits()

    console.log(`Total weight: ${updatedWeight} kg`)
      
  if(validateManifest(manifest)!== {}) {
    console.log(`Validation error: ${containerId}`), 
    console.log(validateManifest(manifest));
    
  }
  }
}

try calling your function

validateManifest()

you would see this error that you need to fix

ReferenceError: containerId is not defined

I called the function at the end of the program, and I saw what the error meant. I put double quotes around containerId in the Object.hasOwn( ) property. It corrected Step 9, but not the rest.

now keep testing

console.log(validateManifest({}))

it should say everything is missing, right?

you can use the function calls mentioned by the tests, and keep testing and fixing the behaviour

Thank you! I figured everything out.