Tell us what’s happening:
I got stuck at test 9, please check my code for a possible solution. All other test met the requirements except for 9.
/**
* 1-4: Normalizes units from lb to kg (1 lb = 0.45 kg)
*/
function normalizeUnits(manifest) {
const newManifest = { ...manifest };
if (newManifest.unit === "lb") {
newManifest.weight = parseFloat((newManifest.weight * 0.45).toFixed(2));
newManifest.unit = "kg";
}
return newManifest;
}
/**
* 5-16: Validates the manifest object
*/
function valid
Your code so far
/**
* 1-4: Normalizes units from lb to kg (1 lb = 0.45 kg)
*/
function normalizeUnits(manifest) {
const newManifest = { ...manifest };
if (newManifest.unit === "lb") {
newManifest.weight = parseFloat((newManifest.weight * 0.45).toFixed(2));
newManifest.unit = "kg";
}
return newManifest;
}
/**
* 5-16: Validates the manifest object
*/
function validateManifest(manifest) {
const result = {};
const fields = ["containerId", "destination", "weight", "unit", "hazmat"];
fields.forEach((field) => {
if (!(field in manifest) || manifest[field] === null || manifest[field] === undefined) {
result[field] = "Missing";
}
});
if (manifest.containerId !== undefined && manifest.containerId !== null) {
if (!Number.isInteger(manifest.containerId) || manifest.containerId <= 0) {
result.containerId = "Invalid";
}
}
if (manifest.destination !== undefined && manifest.destination !== null) {
if (typeof manifest.destination !== "string" || manifest.destination.trim() === "") {
result.destination = "Invalid";
}
}
if (manifest.weight !== undefined && manifest.weight !== null) {
if (typeof manifest.weight !== "number" || Number.isNaN(manifest.weight) || manifest.weight <= 0) {
result.weight = "Invalid";
}
}
if (manifest.unit !== undefined && manifest.unit !== null) {
if (manifest.unit !== "kg" && manifest.unit !== "lb") {
result.unit = "Invalid";
}
}
if (manifest.hazmat !== undefined && manifest.hazmat !== null) {
if (typeof manifest.hazmat !== "boolean") {
result.hazmat = "Invalid";
}
}
return result;
}
/**
* 17-26: Processes manifest, validates, and logs results
*/
function processManifest(manifest) {
const errors = validateManifest(manifest);
if (Object.keys(errors).length === 0) {
console.log(`Validation success: ${manifest.containerId}`);
const normalized = normalizeUnits(manifest);
console.log(`Total weight: ${normalized.weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(errors);
}
}
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