My code is failing tests 21, 25, and 26 on the Cargo Manifest challenge. I’ve implemented both validateManifest and processManifest with the required console.log calls, but I think I am missing an edge case or a formatting requirement. Need help checking why these specific tests aren’t passing.
const cargo = {
containerId: 1,
destination: "AU",
weight: 400,
unit: "lb",
hazmat: true
}
const normalizeUnits = (manifest) => {
const cargo = { ...manifest }
if (cargo.unit === "kg") {
return cargo
} else if (cargo.unit === "lb") {
cargo.weight = Number.parseFloat(cargo.weight * (0.45));
cargo.unit = "kg";
}
return { ...cargo }
}
const validateManifest = (manifest) => {
const cargo = {};
// Safety guard: Ensure manifest is a valid object before inspecting properties
if (!manifest || typeof manifest !== "object" || Array.isArray(manifest)) {
return {
containerId: "Missing",
destination: "Missing",
weight: "Missing",
unit: "Missing",
hazmat: "Missing"
};
}
// 1. containerId (Must be a positive integer > 0)
if (!Object.hasOwn(manifest, "containerId")) {
cargo.containerId = "Missing";
} else if (!Number.isInteger(manifest.containerId) || manifest.containerId <= 0) {
cargo.containerId = "Invalid";
}
// 2. destination (Must be a non-empty string)
if (!Object.hasOwn(manifest, "destination")) {
cargo.destination = "Missing";
} else if (typeof manifest.destination !== "string" || manifest.destination.trim() === "") {
cargo.destination = "Invalid";
}
// 3. weight (Must be a positive finite number > 0)
if (!Object.hasOwn(manifest, "weight")) {
cargo.weight = "Missing";
} else if (typeof manifest.weight !== "number" || !Number.isFinite(manifest.weight) || manifest.weight <= 0) {
cargo.weight = "Invalid";
}
// 4. unit (Must be exactly "lb" or "kg")
if (!Object.hasOwn(manifest, "unit")) {
cargo.unit = "Missing";
} else if (manifest.unit !== "lb" && manifest.unit !== "kg") {
cargo.unit = "Invalid";
}
// 5. hazmat (Must be a boolean)
if (!Object.hasOwn(manifest, "hazmat")) {
cargo.hazmat = "Missing";
} else if (typeof manifest.hazmat !== "boolean") {
cargo.hazmat = "Invalid";
}
// Return empty object if all checks pass, otherwise return the error object
return cargo;
};
console.log(typeof NaN);
console.log(validateManifest({ containerId: 1, destination: "Santa Cruz", weight: 304, unit: "kg", hazmat: false }));
const processManifest = (manifest) => {
const valid = validateManifest(manifest);
// Case 1: Valid manifest (error object has 0 keys)
if (Object.keys(valid).length === 0) {
const norm = normalizeUnits(manifest);
console.log(`Validation success: ${norm.containerId}`);
console.log(`Total weight: ${norm.weight} kg`);
} else {
// Case 2: Invalid manifest
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
};
processManifest({ containerId: -88, destination: "Soledad", weight: NaN });
