Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

If I use a single “=” in the if statement of my normalizeUnits function, step 6 fails because it alters the value of manifest.unit to “lb”. Switching to a stricter equality operand resolves this but causes step 2 to fail.

What could be the issue here?

Your code so far

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

const normalMan = normalizeUnits(manifest);

function normalizeUnits(manifest) {
  if (manifest.unit == "lb") {
    let normalisedManifest = {
      containerId: manifest.containerId,
      destination: manifest.destination,
      weight: manifest.weight*0.45,
      unit: "kg",
      hazmat: manifest.hazmat,
    }
    return normalisedManifest;
  }
}

const valMan = validateManifest(manifest);

function validateManifest(manifest) {
  let validMan = {};
  if (manifest.containerId === undefined) {
    validMan.containerId = "Missing";
  } 
  else if (manifest.containerId < 1 || typeof manifest.containerId != "number") {
    validMan.containerId = "Invalid";
  }
  else {
    validMan.containerId = manifest.containerId;
  }
  if (manifest.destination === undefined) {
    validMan.destination = "Missing";
  }
  else if (typeof manifest.destination != "string") {
    validMan.destination = "Invalid";
  }
  else {
    validMan.destination = manifest.destination
  }
  if (manifest.weight === undefined) {
    validMan.weight = "Missing";
  }
  else if (manifest.weight <= 0) {
    validMan.weight = "Invalid";
  }
  else {
    validMan.weight = manifest.weight;
  }
  if (manifest.unit === undefined) {
    validMan.unit = "Missing";
  }
  else if (manifest.unit != "lb" && manifest.unit != "kg") {
    validMan.unit = "Invalid"; 
  }
  else {
    validMan.unit = manifest.unit;
  }
  if (manifest.hazmat === undefined) {
    validMan.hazmat = "Missing";
  }
  else if (typeof manifest.hazmat != "boolean") {
    validMan.hazmat = "Invalid";
  }
  else {
    validMan.hazmat = manifest.hazmat;
  }
  return validMan;
}

let procMan = processManifest(manifest);

function processManifest(manifest) {

}

console.log(valMan);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Hi @arsenicsys, and welcome to the FCC community!

  • You should implement a function named normalizeUnits with a manifest parameter.
  • The function must not mutate the original manifest object and must always return a new object where weight is normalized to kilograms and unit is set to "kg".

These two parts right here will get you past step 3 being valid. You are correct in your handling of “lb” in normalizeUnits. But what happens when the unit is “kg”? What is returned then? And remember, you cannot return the original manifest. You must return a copy of the manifest object because if you just return the manifest outright, you are passing back a reference to the original manifest.

That will put you past step 3 of this project and into the actual validation aspect of the function.