Tell us what’s happening:
Hello! I am stuck on test case 15 - If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
All the other test cases passed. Can someone point me in the right direction.
Your code so far
function normalizeUnits(manifest){
let wt=manifest.weight;
if (manifest['unit'] == 'lb'){
wt = manifest.weight * 0.45;
}
const normalizedManifest = {
containerId:manifest.containerId,
destination:manifest.destination,
weight:wt,
unit:"kg",
hazmat:manifest.hazmat
}
return normalizedManifest;
}
function validateManifest(manifest){
const validated = {
containerId:'Missing',
destination:'Missing',
weight:'Missing',
unit:'Missing',
hazmat:'Missing'
};
if (typeof manifest !== "object" || manifest === null || Array.isArray(manifest)){
return validated;
}
if(manifest.hasOwnProperty('containerId')){
validated.containerId='Invalid';
if(manifest.containerId!==undefined && manifest.containerId!==null && Number.isInteger(manifest.containerId) && manifest.containerId>0 ){
delete validated.containerId;
}
}
if(manifest.hasOwnProperty('destination')){
validated.destination='Invalid';
if(manifest.destination!==undefined && manifest.destination!==null && typeof manifest.destination === 'string' && manifest.destination.trim()!==""){
delete validated.destination;
}
}
if(manifest.hasOwnProperty('weight')){
validated.weight='Invalid';
if(manifest.weight!==undefined && manifest.weight!==null && Number.isInteger(manifest.weight) && manifest.weight>0){
delete validated.weight;
}
}
if(manifest.hasOwnProperty('unit')){
validated.unit='Invalid';
if(manifest.unit!==undefined && manifest.unit!==null && typeof manifest.unit==='string' && (manifest.unit.toLowerCase() =='kg' || manifest.unit.toLowerCase() =='lb')){
delete validated.unit;
}
}
if(manifest.hasOwnProperty('hazmat')){
validated.hazmat='Invalid';
if(manifest.hazmat!==undefined && manifest.hazmat!==null && (manifest.hazmat==true || manifest.hazmat==false)){
delete validated.hazmat;
}
}
return validated;
}
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; rv:152.0) Gecko/20100101 Firefox/152.0
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator