Build a cargo manifest validator

Tell us what’s happening:

Tests 2, 6 and 18 fail, while the other 24 tests pass, including all the behavior tests for these same functions:

- 2. You should have a function named `normalizeUnits` with a `manifest` parameter.

- 6. You should have a function named `validateManifest` with a `manifest` parameter.

- 18. You should have a function named `processManifest` with a `manifest` parameter.

The hint I get is: “Your code does not contain a declared function named `normalizeUnits` that takes a `manifest` parameter. Define a function using the syntax `function normalizeUnits(manifest) { … }` and try again.” My functions are declared in exactly that form, so I can’t see what’s wrong.

I’ve tried - Plain `function name(manifest) { … }` declarations (not `const`, so test 1 passes)

- `let` arrow functions, after a suggestion on the forum

  • Replacing object spread (`{ …manifest }`) with `Object.assign({}, manifest)`

- Removing `Object.hasOwn` and checking for `=== undefined` instead

- Resetting the lab and pasting the code in fresh

Your code so far

const normalizeUnits = (manifest) => {
  const newManifest = {...manifest};

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

  return newManifest;
}

const validateManifest = (manifest) => {

  const containerId = manifest.containerId;
  const destination = manifest.destination;
  const weight = manifest.weight;
  const unit = manifest.unit;
  const hazmat = manifest.hazmat;

  const validatedManifest = {};

  if(containerId === undefined) {
    validatedManifest.containerId = "Missing";
  } else if (containerId <= 0 || typeof(containerId) != 'number' || 
          !Number.isInteger(containerId) || Number.isNaN(containerId)) {
    validatedManifest.containerId = "Invalid";
  } 

  if (destination === undefined )
  {
    validatedManifest.destination = "Missing";
  } else if (typeof(destination) !== 'string' || destination.trim() === "") {
    validatedManifest.destination = "Invalid";
  }

  if(weight === undefined) {
    validatedManifest.weight = "Missing";
  } else if (weight <= 0 || typeof(weight) != 'number' || 
        Number.isNaN(weight)) {
    validatedManifest.weight = "Invalid";
  }

  if((typeof(unit) === 'string' && unit.trim() === "") ||
     unit === undefined) {
    validatedManifest.unit = "Missing";
  } else if (unit !== "lb" && unit !== "kg" ) {
    validatedManifest.unit = "Invalid";
  }

  if(hazmat === undefined) {
    validatedManifest.hazmat = "Missing";
  } else if (hazmat !== true && hazmat !== false ) {
    validatedManifest.hazmat = "Invalid";
  }

  return validatedManifest;
}

const processManifest = (manifest) => {

  //const manifestValid = validateManifest(manifest);

  if(Object.keys(validateManifest(manifest)).length === 0) {
    console.log(`Validation success: ${manifest.containerId}`);
    const normalized = normalizeUnits(manifest);
    //const weight = normalizeUnits(manifest).weight;
    console.log(`Total weight: ${normalized.weight} ${normalized.unit}`);
  } else {
    console.log(`Validation error: ${manifest.containerId}`);
    console.log(validateManifest(manifest));
  }
}
const myManifest = { containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false };

processManifest(myManifest);

Lesson URL (copy - paste from your browser’s address bar)

Hi @MmathapeloNare,

When I run your code, Test #1 fails with:

You should not declare normalizeUnits, validateManifest, or processManifest using const, since the tests need to reassign them.

And when I make the changes to pass Test #1, your code passes.

Happy coding

Thank you so much, I fixed what needed to be fixed and the tests passed.