Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I have completed the full labs tests that checks if the formatting is correct but I can’t get the last two tests passed. It says log the manifest and the validated manifest. I have logged both but the tests doesn’t seems to pass.

Your code so far

const keys = [
  { name: "containerId", type: "number" },
  { name: "destination", type: "string" },
  { name: "weight", type: "number" },
  { name: "unit", type: "string" },
  { name: "hazmat", type: "boolean" },
];

function normalizeUnits(manifest) {
  const newObj = {};
  Object.assign(newObj, manifest);
  if (newObj.unit === "lb") {
    const convert = {};
    Object.assign(convert, newObj);
    convert.unit = "kg";
    convert.weight = Number((convert.weight * 0.45).toFixed(2));
    return convert;
  }
  return newObj;
}

const validateManifest = (manifest) => {
  const newObj = {};

  //check if missing
  keys.forEach((item, i) => {
    if (manifest[item.name] === undefined) {
      newObj[item.name] = "Missing";
    }
  });

  //check if Invalid
  keys.forEach((item, i) => {
    if (manifest[item.name] === null) {
      newObj[item.name] = "Invalid";
    }
  });

  // check if Invalid type
  keys.forEach((item, i) => {
    if (
      manifest[item.name] !== undefined &&
      typeof manifest[item.name] !== item.type
    ) {
      newObj[item.name] = "Invalid";
    }
  });

  //check if Number is below 0 or nan
  keys.forEach((item, i) => {
    if (typeof manifest[item.name] === "number" && manifest[item.name] <= 0) {
      newObj[item.name] = "Invalid";
    }
  });

  //check if String is empty
  keys.forEach((item, i) => {
    if (
      manifest[item.name] !== undefined &&
      typeof manifest[item.name] === "string" &&
      manifest[item.name].trim() === ""
    ) {
      newObj[item.name] = "Invalid";
    }
  });

  keys.forEach((item, i) => {
    if (
      manifest[item.name] !== undefined &&
      typeof manifest[item.name] === "number" &&
      Number.isNaN(manifest[item.name])
    ) {
      newObj[item.name] = "Invalid";
    }
  });

  if (
    manifest["containerId"] !== undefined &&
    !Number.isInteger(manifest.containerId)
  ) {
    newObj["containerId"] = "Invalid";
  }

  if (
    manifest.unit !== undefined &&
    manifest.unit !== "lb" &&
    manifest.unit !== "kg"
  ) {
    newObj.unit = "Invalid";
  }
  return newObj;
};

function processManifest(manifest) {
  const validatedManifest = validateManifest(manifest);
  if (Object.keys(validatedManifest).length === 0) {
    console.log(`Validation success: ${manifest.containerId}`);
    const normalizedUnits = normalizeUnits(manifest);
    console.log(`Total weight: ${normalizedUnits.weight} kg`)
  } else {
    console.log(`Validation error: ${manifest.containerId}`);
    console.log(validatedManifest);
  }

};

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 OPR/133.0.0.0

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md at main · freeCodeCamp/freeCodeCamp · GitHub

validateManifest was declared using a arrow function that made the test not pass :unamused_face:.

You can still use an arrow function, but it needs to be declared with let instead of const since the tests reassign the functions.