Tell us what’s happening:
Code works, the Chckmark does not. Please help so I could complete this lab :).
Your code so far
function normalizeUnits (manifest)
{
const copy = {...manifest};
if(copy.unit == "lb")
{
copy.unit = "kg";
copy.weight *= 0.45;
return copy;
}
else if(copy.unit == "kg")
{
return copy;
}
else
{
return "Invalid unit measurement";
}
}
function validateManifest (manifest)
{
let copyEmpty = {...manifest}
if (Object.keys(copyEmpty).length == 0)
{
copyEmpty.containerId = "Missing";
copyEmpty.destination = "Missing";
copyEmpty.weight = "Missing";
copyEmpty.unit = "Missing";
copyEmpty.hazmat = "Missing";
return copyEmpty;
}
let copyValid = {...manifest}
if (copyValid.containerId > 0 && typeof copyValid.containerId == "number")
{
if (copyValid.destination != null && typeof copyValid.destination == "string" && copyValid.destination.trim().length != 0)
{
if (copyValid.weight >= 0 && typeof copyValid.weight == "number")
{
if (copyValid.unit == "lb" || copyValid.unit == "kg")
{
if (copyValid.hazmat == true || copyValid.hazmat == false)
{
return new Object();
}
}
}
}
}
function checkNaNorNull (value)
{
if (Number.isNaN(value) || null == value)
{
return true;
}
else
{
return false
}
}
let copy = {...manifest}
if ((copy.containerId <= 0 || typeof copy.containerId != "number" || copy.containerId % 1 != 0 || checkNaNorNull(copy.containerId)) && Object.hasOwn(copy, "containerId"))
{
copy.containerId = "Invalid";
}
else if (!Object.hasOwn(copy, "containerId"))
{
copy.containerId = "Missing";
}
else
{
delete copy.containerId;
}
if ((typeof copy.destination != "string" || copy.destination.trim().length == 0) && Object.hasOwn(copy, "destination"))
{
copy.destination = "Invalid";
}
else if (!Object.hasOwn(copy, "destination"))
{
copy.destination = "Missing";
}
else
{
delete copy.destination;
}
if ((copy.weight < 0 || typeof copy.weight != "number" || checkNaNorNull(copy.weight)) && Object.hasOwn(copy, "weight"))
{
copy.weight = "Invalid";
}
else if (!Object.hasOwn(copy, "weight"))
{
copy.weight = "Missing";
}
else
{
delete copy.weight;
}
if (copy.unit == "kg" || copy.unit == "lb")
{
delete copy.unit;
}
else if (!Object.hasOwn(copy, "unit"))
{
copy.unit = "Missing";
}
else
{
copy.unit = "Invalid";
}
if (copy.hazmat == true || copy.hazmat == false)
{
delete copy.hazmat;
}
else if (!Object.hasOwn(copy, "hazmat"))
{
copy.hazmat = "Missing";
}
else
{
copy.hazmat = "Invalid";
}
return copy;
}
function processManifest (manifest)
{
let processValid = {...manifest}
if (Object.keys(validateManifest(processValid)).length == 0)
{
console.log(`Validation success: ${processValid.containerId}`);
if (processValid.unit == "lb")
{
let weightKg = normalizeUnits(processValid);
return console.log(`Total weight: ${weightKg.weight} kg`);
}
else
{
return console.log(`Total weight: ${processValid.weight} kg`)
}
}
else
{
console.log(`Validation error: ${processValid.containerId}\n`)
}
return console.log(validateManifest(processValid));
}
processManifest({containerId: 1});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator