Tell us what’s happening:
I’m having trouble passing instruction 14. Could anyone give me a hint? I’d really appreciate it ![]()
14. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
Your code so far
let cargo1 = {
containerId: 0,
destination: 55,
unit: "lb ",
hazmat: "sc",
}
const normalizeUnits = (manifest) => {
const newManifest = { ...manifest };
if (newManifest.unit.toLowerCase() === "lb") {
newManifest.weight = Number((newManifest.weight * 0.45).toFixed(2));
newManifest.unit = "kg";
}
return newManifest;
};
const validateManifest = (manifest) => {
const errors = {};
if (manifest.containerId === undefined) {
errors.containerId = "Missing";
} else if (
typeof manifest.containerId !== "number" ||
!Number.isInteger(manifest.containerId) ||
manifest.containerId <= 0
) {
errors.containerId = "Invalid";
}
if (manifest.destination === undefined) {
errors.destination = "Missing";
} else if (
typeof manifest.destination !== "string" ||
manifest.destination.trim() === ""
) {
errors.destination = "Invalid";
}
if (manifest.weight === undefined) {
errors.weight = "Missing";
} else if (
typeof manifest.weight !== "number" ||
Number.isNaN(manifest.weight) ||
manifest.weight <= 0
) {
errors.weight = "Invalid";
}
if (manifest.unit === undefined) {
errors.unit = "Missing";
} else if (
typeof manifest.unit !== "string" ||
!["kg", "lb"].includes(manifest.unit.toLowerCase())
) {
errors.unit = "Invalid";
}
if (manifest.hazmat === undefined) {
errors.hazmat = "Missing";
} else if (typeof manifest.hazmat !== "boolean") {
errors.hazmat = "Invalid";
}
return errors;
};
const processManifest = (manifest) => {
const validationResult = validateManifest(manifest);
if (Object.keys(validationResult).length === 0) {
console.log(`Validation success: ${manifest.containerId}`);
const normalizedManifest = normalizeUnits(manifest);
console.log(`Total weight: ${normalizedManifest.weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
};
processManifest(cargo1)
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