Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

Why am I passing everything except step 14? Is there something wrong or missing from my code?

Your code so far

let valid;

function normalizeUnits(manifest) {
  let obj = {};
  let newObj = Object.assign(obj, manifest);

  if (manifest.unit == "lb") {
    newObj.weight *= 0.45;
    newObj.unit = "kg";
  }

  return newObj;
}

function validateManifest(manifest) {
  let obj = {};

  if (Object.hasOwn(manifest, "containerId") &&
  manifest.containerId > 0 &&
  Number.isInteger(manifest.containerId) &&
  Object.hasOwn(manifest, "destination") &&
  Object.hasOwn(manifest, "weight") &&
  manifest.weight > 0 &&
  !Number.isNaN(manifest.weight) &&
  Object.hasOwn(manifest, "unit") &&
  Object.hasOwn(manifest, "hazmat") &&
  typeof manifest.containerId == "number" &&
  typeof manifest.destination.trim() == "string" &&
  manifest.destination !== "" &&
  typeof manifest.weight == "number" ||
  manifest.unit == "kg" ||
  manifest.unit == "lb" &&
  typeof manifest.hazmat == "boolean") {
    valid = true;
    return obj;
  } else {
    if (!Object.hasOwn(manifest, "containerId")) {
      obj.containerId = "Missing";
    } else if (typeof manifest.containerId !== "number" ||
    manifest.containerId <= 0 ||
    !Number.isInteger(manifest.containerId)) {
      obj.containerId = "Invalid";
    }

    if (!Object.hasOwn(manifest, "destination")) {
      obj.destination = "Missing";
    } else if (typeof manifest.destination !== "string" ||
    manifest.destination.trim() == "") {
      obj.destination = "Invalid";
    }

    if (!Object.hasOwn(manifest, "weight")) {
      obj.weight = "Missing";
    } else if (typeof manifest.weight !== "number" ||
    manifest.weight <= 0 ||
    Number.isNaN(manifest.weight)) {
      obj.weight = "Invalid";
    }

    if (!Object.hasOwn(manifest, "unit")) {
      obj.unit = "Missing";
    } else if (manifest.unit !== "kg" || manifest.unit !== "lb") {
      obj.unit = "Invalid";
    }

    if (!Object.hasOwn(manifest, "hazmat")) {
      obj.hazmat = "Missing";
    } else if (typeof manifest.hazmat !== "boolean") {
      obj.hazmat = "Invalid";
    }

    valid = false;
    return obj;
  }
}

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


console.log(validateManifest({ containerId: 68, destination: "Salinas", weight: -101, unit: "lb", hazmat: true }));
processManifest({ containerId: -68, destination: "Salinas", weight: 101, unit: "lb", hazmat: true });

Your browser information:

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

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Hi @chuacarbonara !

Look at what the console is showing for the two function calls. Does that look like the correct outcome?

Happy coding!

Huh… I don’t know what is happening now… When I called on the two function calls earlier before posting, the console displays it correctly, as in if I make the numbers on either the containerId or weight into 0 or a negative number, the console will actually say Validation error and display the containerId or weight as Invalid. Now it will still say Validation success even when I made the containerId or weight into 0 or a negative number. But then another crazy thing happens. I removed the last two lines of my code (the one shown above on my “Your code so far”) and put this instead:

console.log(validateManifest({ containerId: -88, destination: “Soledad”, weight: NaN }));

processManifest({ containerId: -88, destination: “Soledad”, weight: NaN });

and now the console is displaying it correctly again, as in Validation error -88, and both values of containerId and weight in the returned object are “Invalid”, and if I put in a valid positive number on both, they will no longer be displayed as Invalid. So it works correctly now I guess. But Step 14 still doesn’t pass. I’m confused.

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