Tell us what’s happening:
My code passes all the checks except for number 16. Why am I getting this error?
16. Your validateManifest function should return an object describing missing and/or invalid properties if the input manifest object is not valid.
Your code so far
const testManifest = {
containerId: 1,
destination: "Monterey, California, USA",
weight: 831,
unit: "lb",
hazmat: false
}
function normalizeUnits(manifest){
let copyManifest = {...manifest};
if (copyManifest.unit == "lb"){
copyManifest.weight = copyManifest.weight*0.45;
copyManifest.unit = "kg";
return copyManifest;
} else{
return copyManifest;
}
}
function validateManifest(manifest){
const errorsCount = {};
if (manifest.containerId === undefined) {
errorsCount.containerId = "Missing";
} else if ( typeof manifest.containerId !== "number" ||
!Number.isInteger(manifest.containerId) ||
manifest.containerId <= 0){
errorsCount.containerId = "Invalid";
}
if (manifest.destination === undefined) {
errorsCount.destination = "Missing";
} else if (typeof manifest.destination !== 'string' || manifest.destination.trim() === ""){
errorsCount.destination = "Invalid";
}
if (manifest.weight === undefined) {
errorsCount.weight = "Missing";
} else if ( typeof manifest.weight !== "number" ||
Number.isNaN(manifest.weight) ||
manifest.weight <= 0){
errorsCount.weight = "Invalid";
}
if (manifest.unit === undefined) {
errorsCount.unit = "Missing";
} else if ((typeof manifest.unit !== 'string') || (!["kg", "lb"].includes(manifest.unit.toLowerCase()))){
errorsCount.unit = "Invalid";
}
if (manifest.hazmat === undefined) {
errorsCount.hazmat = "Missing";
} else if (typeof manifest.hazmat !== 'boolean'){
errorsCount.hazmat = "Invalid";
}
if (Object.keys(errorsCount).length === 0) {
return {}
}
return errorsCount;
}
function processManifest(manifest) {
if (Object.keys(validateManifest(manifest)).length === 0){
let weight = normalizeUnits(manifest).weight;
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
}
processManifest({containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator