Build a Cargo Manifest Validator - Step 21

Tell us what’s happening:

Hi there! All steps but 21 pass for me and I can’t figure out why. This is Step 21:

If the input manifest object is valid, your processManifest function should log a success message with the object’s containerId, and then log the object’s weight in kilograms. You should use normalizeUnits() for the conversion and have two console.log() calls.

Your code so far

let manifest = {
  containerId: 68,
  destination: "Salinas",
  weight: 101,
  unit: "lb",
  hazmat: true
}

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

console.log(normalizeUnits(manifest));
console.log(manifest);

function validateManifest(manifest) {
  const updatedManifest = {...manifest};

  const containerIDValid = (
    Number.isInteger(updatedManifest.containerId)
    && updatedManifest.containerId > 0) ? (delete updatedManifest.containerId) :
    !Object.hasOwn(updatedManifest, 'containerId') ? updatedManifest.containerId = "Missing" :
    updatedManifest.containerId = "Invalid";

  const destinationValid = (typeof updatedManifest.destination === 'string' && updatedManifest.destination.trim() ) ? (delete updatedManifest.destination) : !Object.hasOwn(updatedManifest, 'destination') ? updatedManifest.destination = "Missing" :
  updatedManifest.destination = "Invalid";

  const weightValid = (updatedManifest.weight > 0) ? (delete updatedManifest.weight) :
  !Object.hasOwn(updatedManifest, 'weight') ? updatedManifest.weight = "Missing" :
  updatedManifest.weight = "Invalid";

  const unitValid = (updatedManifest.unit === "kg" || updatedManifest.unit === "lb") ? (delete updatedManifest.unit) :
  !Object.hasOwn(updatedManifest, 'unit') ? updatedManifest.unit = "Missing" :
  updatedManifest.unit = "Invalid";

  const hazmatValid = (typeof updatedManifest.hazmat === 'boolean') ? delete updatedManifest.hazmat :
  !Object.hasOwn(updatedManifest, 'hazmat') ? updatedManifest.hazmat = "Missing" :
  updatedManifest.hazmat = "Invalid";

  return updatedManifest;
}

console.log(validateManifest(manifest));

const updatedManifest = validateManifest(manifest);

console.log(validateManifest(updatedManifest));

manifest = {containerId: 3.50}
console.log(validateManifest(manifest));

manifest = {destination: "  "}
console.log(validateManifest(manifest));

manifest = {weight: NaN}
console.log(validateManifest(manifest));

manifest = {
  containerId: 55,
  destination: "Carmel",
  weight: 400,
  unit: "lb",
  hazmat: false
}

function processManifest (manifest) {
  const validatedManifest = validateManifest(manifest);

  if (Object.keys(validatedManifest).length === 0) {
    console.log(`Validation success: ${manifest.containerId}`);
    console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`);
 } else {
    console.log(`Validation error: ${manifest.containerId}`);
    console.log(validatedManifest); 
 }
}

processManifest(manifest);

manifest = {
  containerId: -88,
  destination: "Soledad",
  weight: NaN,
}

processManifest(manifest); 

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

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

hello!

are you sure your normalizeUnits is working correctly?

Yes, I’m getting the correct results in the output. And the rest of my steps all pass.

What if manifest.unit is “kg”? What does normalizeUnits return then?

please reconsider what = does. should = be used to check a condition inside an if statement?

It returned undefined. Thanks for pointing that out! I’ve fixed that now. :slight_smile:

Step 3 started failing after I resolved the issues mentioned in this thread. To fix that I made sure that the function’s ‘return’ was not ever the original manifest, but rather the updatedManifest, only.

Thank you for pointing that out! I’ve fixed that.

I’m curious why it returned the correct results even though I used the incorrect operator. Do you know?

not very sure. could be due to test configurations.

you were making so that manifest.unit had that value every time, and it’s not true that it was not failing, you were only passing the tests weremanifest.unit was "lb" from the start