Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

My function returns an object, but the error in step 14 indicates that it doesn’t.

Your code so far

function normalizeUnits (manifest){
  const copia = {...manifest};

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

  return copia;
}

function validateManifest(manifest) {
  const resultado = {};

  if (!Object.hasOwn(manifest, "containerId")) {
    resultado.containerId = "Missing";
  } else if (!Number.isInteger(manifest.containerId) || manifest.containerId < 1) {
    resultado.containerId = "Invalid";
  } else {
    resultado.containerId = manifest.containerId;
  }

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

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

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

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

  const todoValido =
    Number.isInteger(resultado.containerId) &&
    typeof resultado.destination === "string" &&
    typeof resultado.weight === "number" &&
    (resultado.unit === "kg" || resultado.unit === "lb") &&
    typeof resultado.hazmat === "boolean";

  return todoValido ? {} : resultado;
}

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

i don’t see any log statements in your code. Did you try to do any debugging?

What did you try to confirm your code works?

Edit: You could for example add this to your code to check what is the result and compare it to your understanding of what should be returned.

console.log(validateManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false }));

(if you add log statements you might be able to figure out the issue)

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