Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I can’t seem to pass beyond step 18 and I can’t figure out what is wrong.
When I test the function processManifest agaisnt the console the value undefined always at the end.

Your code so far

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

const normalizeUnits = (manifest) => {
  const copyManifest = {... manifest};
  if (copyManifest.unit === "lb") {
    copyManifest.unit = "kg";
    copyManifest.weight = copyManifest.weight * 0.45;
  }
  return copyManifest
};

// console.log(cargo1);

const cargo2 = {
  containerId: 1,
  destination: "Santa Cruz",
  weight: 304,
  unit: "kg",
  hazmat: false
}

const validateManifest = (manifest) => {
  const newManifest = {};
  if ("containerId" in manifest == false) {
    newManifest.containerId = "Missing"
  } else if (manifest.containerId == null || manifest.containerId == undefined || manifest.containerId <1 || !Number.isInteger(manifest.containerId)) {
    newManifest.containerId = "Invalid"
  };
  if ("destination" in manifest == false) {
    newManifest.destination = "Missing"
  } else if (typeof manifest.destination != "string" || manifest.destination.trim() == 0) {
    newManifest.destination = "Invalid"
  };
  if ("weight" in manifest == false) {
    newManifest.weight = "Missing"
  } else if (Number.isNaN(manifest.weight) || typeof manifest.weight != "number" || manifest.weight<=0) {
    newManifest.weight = "Invalid"
  };
  if ("unit" in manifest == false) {
    newManifest.unit = "Missing"
  } else if (manifest.unit != "lb" && manifest.unit != "kg") {
    newManifest.unit = "Invalid"
  };
  if ("hazmat" in manifest == false) {
    newManifest.hazmat = "Missing"
  } else if (typeof manifest.hazmat != "boolean") {
    newManifest.hazmat = "Invalid"
  };
  return newManifest;
};

//console.log(validateManifest(cargo2));

const isEmpty = (manifest) => {
  if (Object.keys(validateManifest(manifest)).length === 0) {
    return true;
  } else {
    return false;
  }
};

const processManifest = (manifest) => {
  if (isEmpty(manifest) === true) {
    console.log("Validation success: ${containerId}");
    console.log("Total weight: ${normalizeUnits(manifest.weight)} kg");
  } else {
    console.log("Validation error: ${containerId}");
    console.log(validateManifest(manifest));
  }
};

const cargo3 = {
  containerId: -88,
  destination: "Soledad",
  weight: NaN
}

console.log(processManifest(cargo3))





Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:151.0) Gecko/20100101 Firefox/151.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

Welcome to the forum @nt.brunoaraujo,

When you log the function call indicated in Test #18, what do you see in the console? Does that match what it says that function call should return?

Please review this theory lecture about how to create template literals:
Working with Strings in JavaScript - What Are Template Literals, and What Is String Interpolation? | Learn | freeCodeCamp.org

Happy coding

Thanks a lot for the hint. I fixed the code and now I pass in all tests. However, if I test any of the manifest examples with the function processManifest against the console, the value undefined always show up at the end.

Fixed code:

const processManifest = (manifest) => {

  if (isEmpty(manifest) === true) {

const normal = normalizeUnits(manifest);

    console.log(`Validation success: ${manifest.containerId}`);

    console.log(`Total weight: ${normal.weight} kg`);

} else {

    console.log(`Validation error: ${manifest.containerId}`);

    console.log(validateManifest(manifest));

}

};

Result with cargo2:

console.log(processManifest(cargo2));

/*
Validation success: 1
Total weight: 304 kg
undefined
*/

Yes. Undefined shows because processManifest() has no return statement, but that’s okay in this case. Functions don’t have to have a return value, although most do.

That’s good to know, thanks a lot for the help.