Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

My code isn’t working, but I’m not sure why? Stuck at step 14, specifically, the other stuff I’ll debug on my own. Hven’t gotten anything helpful from console.log().

Your code so far

function normalizeUnits(manifest) {
  const copy = {
    containerId: manifest.containerId,
    destination: manifest.destination,
    weight: manifest.weight,
    unit: manifest.unit,
    hazmat: manifest.hazmat
  }
  if(manifest.unit === 'lb') {
    copy.weight *= 0.45
    copy.unit = 'kg'
  }
  return copy
}

function validateManifest(manifest) {
    const copy = {
    containerId: manifest.containerId,
    destination: manifest.destination,
    weight: manifest.weight,
    unit: manifest.unit,
    hazmat: manifest.hazmat
  }
  let pass = true;
  if(manifest.containerId === undefined || Object.hasOwn(manifest, 'containerId') === false) {
    copy.containerId = 'Missing'
    pass = false;
  }
  else if(manifest.containerId <= 0 || typeof manifest.containerId !== 'number' || Number.isInteger(manifest.containerId) === false) {
    copy.containerId = 'Invalid';
    pass = false;
  }
  if(manifest.destination === undefined || Object.hasOwn(manifest, 'destination') === false) {
  copy.destination = 'Missing';
  pass = false;
  }
  else if(typeof manifest.destination !== 'string' || manifest.destination.trim() === '') {
    copy.destination = 'Invalid'
    pass = false;
  }
  if(typeof manifest.weight === undefined || Object.hasOwn(manifest, 'weight') === false) {
    copy.weight = 'Missing';
    pass = false;
  }
  else if(typeof manifest.weight !== 'number' || manifest.weight < 1 || Number.isNaN(manifest.weight)) {
    copy.weight = 'Invalid';
    pass = false;
  } 
  if(typeof manifest.unit === undefined || Object.hasOwn(manifest, 'unit') === false) {
    copy.unit = 'Missing';
    pass = false;
  }
  else if(manifest.unit !== 'kg' && manifest.unit !== 'lb') {
    copy.unit = 'Invalid';
    pass = false;
  }
  if(typeof manifest.hazmat === undefined || Object.hasOwn(manifest, 'hazmat') === false) {
    copy.hazmat = 'Missing';
    pass = false;
  }
  else if(typeof manifest.hazmat !== 'boolean') {
    copy.hazmat = 'Invalid';
    pass = false;
  }
  if(pass) {
    return {}
  }
  else {
    return copy
  }
}

function processManifest(manifest) {
  if(validateObject(manifest) === {}) {
    console.log(`Validation success: ${containerId}`);
    console.log(`Total weight: ${normalizeUnits(manifest)} kg`);
  }
  else {
    console.log(`Validation error: ${containerId}`); 
    console.log(validateManifest(manifest));
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 OPR/128.0.0.0

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Welcome to the forum @Meep_13 !

Is validateObject() a function in your code?

And that is not the way to check if your function returns an empty object. Test it. Log validateManifest(manifest). Then log validateManifest(manifest) === {}. Is that what you expect to see?

Test #14 fails when something is borked in your validateManifest function, so please review the user stories carefully to make sure you are implementing them correctly.

Happy coding!

Right… I found that issue in the processManifest function right after I posted this, but I still can’t find the issue that’s in the actual validateManifest function itself…

Test your code. Try calling it like this:

processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false })

and keep changing the values to see what it’s doing.

Tried that… still can’t find the issue.

Oh? And what did you see in the console when you tried that? Did you make any changes to update your code?

It wasn’t working, but I can’t figure out where the issue itself is outside of just the function. Number.isNaN doesn’t seem like it’s working properly, either? It isn’t setting the copy’s weight to say Invalid…

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.