Here is my code and I am stuck with “Test 15: If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties”.
// Copy Object
function copyObject(manifest, obj)
{
// Adding properties to new object
obj.containerId = manifest.containerId;
obj.destination = manifest.destination;
obj.weight = manifest.weight;
obj.unit = manifest.unit;
obj.hazmat = manifest.hazmat;
}
// Normalize Units
function normalizeUnits(manifest)
{
// Create a new object
let obj = new Object();
// Copying Object
copyObject(manifest, obj);
// Conversion from 'lb' to 'kg'
if(obj.unit == "lb")
{
obj.unit = "kg";
obj.weight = obj.weight * 0.45;
}
return obj;
}
// Validate Manifest
function validateManifest(manifest)
let obj = new Object(manifest);
let check_obj = new Object();
if(obj.hasOwnProperty("containerId") == false)
{
check_obj.containerId = "Missing";
}
else if(!Number.isInteger(obj.containerId) || obj.containerId <= 0 || obj.containerId == null)
{
check_obj.containerId = "Invalid";
}
if(obj.hasOwnProperty("destination") == false)
{
check_obj.destination = "Missing";
}
else if(obj.destination == '' || obj.destination == ' ' || typeof(obj.destination) == "number" || obj.destination == null)
{
check_obj.destination = "Invalid";
}
if(obj.hasOwnProperty("weight") == false)
{
check_obj.weight = "Missing";
}
else if(obj.weight <= 0 || obj.weight == null || isNaN(obj.weight))
{
check_obj.weight = "Invalid";
}
if(obj.hasOwnProperty("unit") == false)
{
check_obj.unit = "Missing";
}
else if(obj.unit != 'lb' && obj.unit != 'kg')
{
check_obj.unit = "Invalid";
}
if(obj.hasOwnProperty("hazmat") == false)
{
check_obj.hazmat = "Missing";
}
else if(obj.hazmat != true && obj.hazmat != false)
{
check_obj.hazmat = "Invalid";
}
return check_obj;
}
function processManifest(manifest)
{
let newManifest = validateManifest(manifest) ;
if (!Object.hasOwn(manifest, "containerId") || !Object.hasOwn(manifest, "destination") || !Object.hasOwn(manifest, "weight") || !Object.hasOwn(manifest, "unit") || !Object.hasOwn(manifest, "hazmat"))
{
console.log(`Validation error: ${manifest.containerId}`);
console.log(newManifest);
}
else
{
console.log(`Validation success: ${manifest.containerId}`);
newManifest = normalizeUnits(manifest);
console.log(`Total weight: ${newManifest.weight} kg`);
}
}
const m_obj = {
containerId: 1,
destination: "Monterey, California, USA",
weight: 831,
unit: "lb",
hazmat: false
}
processManifest(m_obj);