Tell us what’s happening:
My code can’t pass the test 21, 25, 26.
Kindly help me check my code and help me find what I did wrong.
Your code so far
const normalizeUnits = (manifest) => {
const copyManifest = {...manifest};
if (copyManifest.unit === "lb"){
copyManifest["unit"] = "kg";
copyManifest.weight = copyManifest.weight * 0.45;
copyManifest.weight = Math.round(copyManifest.weight*100)/100;
}
return copyManifest;
}
const validateManifest = (manifest) => {
const missingEntries = {};
if (!Object.hasOwn(manifest,"containerId")){
missingEntries["containerId"] = "Missing"
} else if (! (Number.isInteger(manifest.containerId) && manifest.containerId>0)){
missingEntries["containerId"] = "Invalid";
}
if (!(Object.hasOwn(manifest,"destination"))){
missingEntries["destination"] = "Missing";
} else if (typeof manifest["destination"] !== "string" || !(manifest["destination"].trim().length > 0)){
missingEntries["destination"] = "Invalid";
}
if (!(Object.hasOwn(manifest,"weight"))){
missingEntries["weight"] = "Missing";
} else if(!(manifest.weight>0 && !Number.isNaN(manifest.weight))){
missingEntries["weight"] = "Invalid";
}
if(!(Object.hasOwn(manifest,"unit"))){
missingEntries["unit"] = "Missing";
} else if(!((manifest.unit=="kg"||manifest.unit=="lb") && typeof manifest.unit == "string")){
missingEntries["unit"] = "Invalid";
}
if (!(Object.hasOwn(manifest,"hazmat"))){
missingEntries["hazmat"] = "Missing";
}else if(!(typeof manifest.hazmat == "boolean")){
missingEntries["hazmat"] = "Invalid";
}
return missingEntries;
}
const processManifest = (manifest) => {
const validatedManifest = validateManifest(manifest);
if (Object.keys(validatedManifest).length === 0){
const normalizedManifest = normalizeUnits(manifest);
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${normalizedManifest.weight} ${normalizedManifest.unit}`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validatedManifest);
}
}
processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false });
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator