Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

What wrong with my code, I cannot pass story number 21, 25 and 26

Your code so far

const cargo1 = {
  containerId: 1,
  destination: "Monterey, California, USA",
  weight: 8,
  unit: "kg",
  hazmat: false
};

const cargo2 = {
  containerId: 2,
  destination: "Chicago, USA",
  weight: 300,
  unit: "lb",
  hazmat: true
};

const cargo3 = {
  containerId: 3,
  destination: "",
  weight: "heavy",
  unit: "lb",
  hazmat: true
};

const normalizeUnits = function(manifest) {
  if (manifest.unit === "lb"){
    return {
      ...manifest,
      weight: manifest.weight * 0.45,
      unit: "kg"
    };
  }
  return {...manifest};
}


const validateManifest = function(manifest) {
  const missing = {}
  if (!manifest.hasOwnProperty("containerId")){
    missing.containerId = "Missing"
  }else if (typeof(manifest.containerId) !== "number" || manifest.containerId <= 0 || !Number.isInteger(manifest.containerId)){
    missing.containerId = "Invalid"
  }
  if (!manifest.hasOwnProperty("destination")){
    missing.destination = "Missing"
  }else if (typeof(manifest.destination) !== "string" || manifest.destination.trim().length === 0){
    missing.destination = "Invalid"
  }
  if (!manifest.hasOwnProperty("weight")){
    missing.weight = "Missing"
  }else if (typeof(manifest.weight) !== "number" || manifest.weight <= 0 || Number.isNaN(manifest.weight)){ 
    missing.weight = "Invalid"
  } 
  if (!manifest.hasOwnProperty("unit")){
    missing.unit = "Missing"
  }else if (typeof(manifest.unit) !== "string" || manifest.unit.trim().length === 0){
    missing.unit = "Invalid"
  }else if (manifest.unit !== "kg" && manifest.unit !== "lb"){
    missing.unit = "Invalid"
  }
  if (!manifest.hasOwnProperty("hazmat")){
    missing.hazmat = "Missing"
  }else if (typeof(manifest.hazmat) !== "boolean"){
    missing.hazmat = "Invalid"
  }
  return {...missing}
}


const processManifest = function(manifest) {
  let newManifest= validateManifest(manifest)
  if(newManifest.containerId === "Invalid" || newManifest.containerId === "Missing"){
    console.log(`Validation error: ${manifest.containerId}`)
    console.log(newManifest);
  }else {
    console.log(`Validation success: ${normalizeUnits(manifest)}`)
    console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`)
  }
} 

console.log(processManifest({ containerId: -88, destination: "Soledad", weight: NaN }))

Your browser information:

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

Welcome to the forum @nurmuliadi024,

What if weight is invalid/missing or unit is invalid/missing or … ??

Happy coding

I change my code now to like this:

if (Object.keys(error).length === 0)

but still can’t pass the story number 21, 25 and 26

Where is error coming from?

I already Fiks it It was because I’m using different function, I think u should change the story for more specific because the story only say “You should implement a function named normalizeUnits.” . I’am using const nomalizeUnits = function(manifest), it was wrong the sistem not accept it, u can try if u want

I suggest reviewing this theory lecture about how to create either a regular function or arrow function. Your syntax is incorrect.

Working with Functions - What Are Arrow Functions, and How Do They Work? | Learn | freeCodeCamp.org