Tell us what’s happening:
Hello, my code can’t seem to pass the test 15, saying that is the input manifest is not valid, the validate manifest should return and object describing those properties. Can someone see where I made a mistake? ![]()
Your code so far
function normalizeUnits(manifest) {
let copy = {
containerId: manifest.containerId,
destination: manifest.destination,
weight: manifest.unit === "kg" ? manifest.weight : Number(parseFloat(manifest.weight * 0.45)),
unit: "kg",
hazmat: manifest.hazmat
};
return copy;
};
function validateManifest(manifest) {
const invalidProps = {};
if (manifest.containerId === undefined) {
invalidProps.containerId = "Missing";
} else if (typeof manifest.containerId !== "number" || manifest.containerId <= 0 || !Number.isInteger(manifest.containerId)) {
invalidProps.containerId = "Invalid";
};
if (manifest.destination === undefined) {
invalidProps.destination = "Missing";
} else if (typeof manifest.destination !== "string" || manifest.destination.trim() === "") {
invalidProps.destination = "Invalid";
};
if (manifest.weight === undefined) {
invalidProps.weight = "Missing";
} else if (typeof manifest.weight !== "number" || manifest.weight <= 0 || Number.isNaN(manifest.weight)) {
invalidProps.weight = "Invalid";
};
if (manifest.unit === undefined) {
invalidProps.unit = "Missing";
} else if (typeof manifest.unit !== "string" || (manifest.unit.toLowerCase() !== "kg" && manifest.unit.toLowerCase() !== "lb")) {
invalidProps.unit = "Invalid";
};
if (manifest.hazmat === undefined) {
invalidProps.hazmat = "Missing";
} else if (typeof manifest.hazmat !== "boolean") {
invalidProps.hazmat = "Invalid"
}
return invalidProps;
};
function processManifest(manifest) {
let normalizedManifest = normalizeUnits(manifest);
if (Object.keys(validateManifest(manifest)).length === 0) {
let t_weight = normalizedManifest.weight
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${t_weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
};
};
console.log(processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false}))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.4 Safari/605.1.15
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator