Tell us what’s happening:
I am working on ‘Build a Cargo Manifest Validator’ challenge included in the ‘JavaScript Certification’ course. My code passes all the tests except for the one at # 14: “If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.”
I have tried all that I could think of but cannot seem to go past this test. When searching on the forums, I noticed a recent similar post but still need help / hint. Please help.
Your code so far
function normalizeUnits(manifest) {
const m1 = {};
m1.containerId = manifest.containerId;
m1.destination = manifest.destination;
if (manifest.unit == 'lb'){
m1.weight = manifest.weight * 0.45;
m1.unit = 'kg';
}else {
m1.weight = manifest.weight;
m1.unit = manifest.unit;
}
m1.hazmat = manifest.hazmat;
return m1;
}
function validateManifest(manifest){
const m1 = {};
if ((typeof(manifest.containerId) != 'number') && (!(manifest.containerId)) ||(manifest.containerId === 'undefined') ){
m1.containerId = "Missing";
} else if (!(Number.isInteger(manifest.containerId)) || manifest.containerId <= 0) {
m1.containerId = "Invalid";
}
if (!(manifest.destination)){
m1.destination = "Missing";
} else if (!(typeof(manifest.destination) == 'string') || (manifest.destination.trim() == "")) {
m1.destination = "Invalid";
}
if (!(manifest.weight) && (!Number.isNaN(manifest.weight))){
m1.weight = "Missing";
} else if (!(typeof(manifest.weight) == 'number') || Number.isNaN(manifest.weight) || manifest.weight <= 0) {
m1.weight = "Invalid";
}
if (!(manifest.unit)){
m1.unit = "Missing";
} else if (!(manifest.unit == 'lb' || manifest.unit == 'kg')) {
m1.unit = "Invalid";
}
if (typeof(manifest.hazmat) == 'undefined'){
m1.hazmat = "Missing";
} else if (!(typeof(manifest.hazmat) == 'boolean')) {
m1.hazmat = "Invalid";
}
return m1;
}
function processManifest(manifest) {
if (Object.keys(validateManifest(manifest)).length == 0) {
console.log("Validation success: " + manifest.containerId);
console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
}
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 Edg/146.0.0.0
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator