Tell us what’s happening:
I got all steps checked except for 14th and I don’t know why. What I want from this help post is not the solution or a hint, but the reason why this happens because from my understanding, 14th step appeared to me like an overall validation check of other several steps in this lab if you get what I mean.
Your code so far
const exampleCargoManifest = {
containerId: 1,
destination: "Monterey, California, USA",
weight: 831,
unit: "lb",
hazmat: false
};
function normalizeUnits(manifest) {
const copy = structuredClone(manifest);
if (manifest.unit === "kg") {
return copy;
}
copy.weight *= 0.45;
copy.unit = "kg";
return copy;
}
function validateManifest(manifest) {
let obj = {};
for (const key in exampleCargoManifest) {
if (!manifest.hasOwnProperty(key)) {
obj[key] = "Missing";
} else {
switch (key) {
case "containerId":
if (typeof manifest[key] !== "number" || Number.isNaN(manifest[key]) || (!Number.isInteger(manifest[key]) || manifest[key] < 1)) {
obj[key] = "Invalid"
}
break;
case "destination":
if (typeof manifest[key] !== "string" || (typeof manifest[key] === "string" && manifest[key].trim().length === 0)) {
obj[key] = "Invalid";
}
break;
case "weight":
if (typeof manifest[key] !== "number" || Number.isNaN(manifest[key]) || manifest[key] < 0) {
obj[key] = "Invalid"
}
break;
case "unit":
if (typeof manifest[key] !== "string" || (manifest[key] !== "lb" && manifest[key] !== "kg"))
obj[key] = "Invalid";
break;
case "hazmat":
if (typeof manifest[key] !== "boolean") {
obj[key] = "Invalid";
}
break;
}
}
}
return obj;
}
function processManifest(manifest) {
let validationObj = validateManifest(manifest);
if (Object.keys(validationObj).length === 0) {
console.log(`Validation success: ${manifest.containerId}`);
manifest = normalizeUnits(manifest);
console.log(`Total weight: ${manifest.weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validationObj);
}
}
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