Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

I am puzzled in how/why my already saved code for this lab (I hope anyone helping me can check it out) is unable to pass the test #16 “validateManifest function should return an object” but it shows as passed all the others. Unfortunately there is not much description in the actual test #16 in order to debug more accurately.

Your code so far

function normalizeUnits (manifest) {
    let normMani = {...manifest};
    if (normMani.unit === "lb") {
	normMani.weight *= 0.45;
	normMani.unit = "kg";
    }
    return normMani;
};

function validateManifest (manifest) {
    let repMani = {};
    if (!(typeof manifest === "object")) {
	repMani.containerId = "Missing";
	repMani.destination = "Missing";
	repMani.weight = "Missing";
	repMani.unit = "Missing";
	repMani.hazmat = "Missing";
	return repMani;
    }
    if (!manifest.hasOwnProperty("containerId")){
	repMani.containerId = "Missing";
    } else if (!(typeof manifest.containerId === "number" &&
		 Number.isInteger(manifest.containerId) &&
		 manifest.containerId > 0)) {
	repMani.containerId = "Invalid";
    }
    if (!manifest.hasOwnProperty("destination")) {
	repMani.destination = "Missing";
    } else if (!(typeof manifest.destination === "string" && manifest.destination.trim() !== "")) {
	repMani.destination = "Invalid";
    }
    if (!manifest.hasOwnProperty("weight")) {
	repMani.weight = "Missing";
    } else if (!(typeof manifest.weight === "number" && manifest.weight >= 0)) {
	repMani.weight = "Invalid";
    }
    if (!manifest.hasOwnProperty("unit")) {
	repMani.unit = "Missing";
    } else if (!(typeof manifest.unit === "string" && (manifest.unit === "kg" || manifest.unit === "lb"))) {
	repMani.unit = "Invalid";
    }
    if (!manifest.hasOwnProperty("hazmat")) {
	repMani.hazmat = "Missing";
    } else if (!(typeof manifest.hazmat === "boolean")) {
	repMani.hazmat = "Invalid";
    }
    return repMani;
};


function processManifest (manifest) {
    let probeMani =  validateManifest(manifest);
    if (probeMani.hasOwnProperty("containerid") ||
	probeMani.hasOwnProperty("destination") ||
	probeMani.hasOwnProperty("weight") ||
	probeMani.hasOwnProperty("unit") ||
	probeMani.hasOwnProperty("hazmat")) {
	console.log(`Validation error: ${manifest.containerId}`);
	console.log(probeMani);
    } else {
	let normMani = normalizeUnits(manifest);
	console.log(`Validation success: ${normMani.containerId}`);
	console.log(`Total weight: ${normMani.weight} kg`);
    }
};

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:154.0) Gecko/20100101 Firefox/154.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 @rontiv,

To help you debug, try testing your code like this:

console.log(validateManifest({
  containerId: 0,
  destination: "",
  weight: 0,
  unit: "kg",
  hazmat: false
}))

Hope that helps.

Thanks @dhess it is not that I don’t know how to debug what I was trying to state is that for the particular test #16 its description is very generic so I fail to know how it is actually tested and to attempt to replicate such test in order to debug it properly. For your suggestion I get as result “{ containerId: “Invalid”, destination: “Invalid” }” which is what is expected as I understand, so I am still wondering how my code is failing…

what about weight? are you sure it is a valid value?

It was indeed an incorrect check for “weight >= 0” changing it to “weight > 0” fix my issue. This is in accordance to the lag instruction where it is mentioned that the weight should be “a positive number”. Thanks @ILM and also @dhess for your help.