Tell us what’s happening:
My code does not pass requirements 21, 25, and 26 despite returning all the right results.
Your code so far
const normalizeUnits = (manifest) => {
if(manifest.unit === "lb"){
const manifestCopy = {...manifest}
const weight = manifestCopy.weight;
manifestCopy.weight = weight*0.45;
manifestCopy.unit = "kg";
return manifestCopy;
}
else {
return {...manifest};
}
}
const validateManifest = (manifest) => {
let errors = {};
if(Object.hasOwn(manifest, "containerId")){
if(manifest.containerId === null || manifest.containerId <= 0 ||
typeof manifest.containerId !== "number" || manifest.containerId % 1 !== 0 || manifest.containerId.toString()?.includes('.'))
{
errors.containerId = "Invalid";
}
} else {
errors.containerId = "Missing";
}
if(Object.hasOwn(manifest, "destination")){
if (typeof manifest.destination !== "string" || manifest.destination === null || manifest.destination.trim() === "")
{
errors.destination = "Invalid";
}
} else {
errors.destination = "Missing";
}
if (Object.hasOwn(manifest, "weight")){
if(Number.isNaN(manifest.weight)||
manifest.weight === null || manifest.weight <= 0)
{
errors.weight = "Invalid";
}
} else {
errors.weight = "Missing";
}
if(Object.hasOwn(manifest, "unit")){
if (manifest.unit === null || typeof manifest.unit !== "string" || manifest.unit !== "lb" && manifest.unit !== "kg")
{
errors.unit = "Invalid";
}
} else {
errors.unit = "Missing";
}
if(Object.hasOwn(manifest, "hazmat")){
if (manifest.hazmat === null || typeof manifest.hazmat !== "boolean")
{
errors.hazmat = "Invalid";
}
} else {
errors.hazmat = "Missing";
}
return errors;
}
const processManifest = (manifest) => {
if(manifest === null || manifest === undefined) manifest = {};
const errors = validateManifest(manifest);
if (Object.keys(errors).length === 0){
const normalizedManifest = normalizeUnits(manifest);
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${normalizedManifest.weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
}