Issue:
My code is already working with the required output but somehow I can’t pass step 21, 25 and 26 which required same result. Kindly help me check my code, what did I do wrong.
const maniObject = {
containerId: 68,
destination: "Salinas",
weight: 101,
unit: "lb",
hazmat: true };
function normalizeUnits(manifest){
const copy = {...manifest};
const outputcopy = {};
if(copy.unit === "lb"){
outputcopy.containerId = copy.containerId;
outputcopy.destination = copy.destination;
outputcopy.weight = copy.weight * 0.45;
outputcopy.unit = "kg";
outputcopy.hazmat = copy.hazmat;
return outputcopy;
};
console.log()
return copy;
};
const invalidObj = {
containerId: 0,
destination: 405,
weight: -84,
unit: "pounds",
hazmat: "no" };
const valObject = {
containerId: 3,
destination: "asdf",
weight: 0,
unit: "kg",
hazmat: false } ;
function validateManifest(manifest){
const copy = {...manifest};
const errors = {};
if (typeof copy !== "object") {
errors.containerId = "Invalid";
errors.destination = "Invalid";
errors.weight = "Invalid";
errors.unit = "Invalid";
errors.hazmat = "Invalid";
}
if(!copy.hasOwnProperty("containerId") || copy.containerId === ""){
errors.containerId = "Missing";
}else if(
copy.containerId === null ||
copy.containerId <= 0 ||
isNaN(copy.containerId)
){ errors.containerId = "Invalid";
}else if(!Number.isInteger(copy.containerId)){
errors.containerId = "Invalid";
errors.destination = "Missing";
errors.weight = "Missing";
errors.unit = "Missing"
errors.hazmat = "Missing";
};
if(!copy.hasOwnProperty("destination") ||
copy.destination === "")
{
errors.destination = "Missing"
}
else if(typeof copy.destination !== "string" || copy.destination.trim() === ""){
errors.destination = "Invalid";
};
if(!copy.hasOwnProperty("weight") || copy.weight === ""){
errors.weight = "Missing"
}else if(typeof copy.weight !== "number" || copy.weight <= 0 || isNaN(copy.weight)){
errors.weight = "Invalid";
};
if(!copy.hasOwnProperty("unit") || copy.unit === ""){
errors.unit = "Missing"
}else if(copy.unit !== "kg" && copy.unit !== "lb" || typeof copy.unit !== "string"){
errors.unit = "Invalid";
};
if(!copy.hasOwnProperty("hazmat") || copy.hazmat === ""){
errors.hazmat = "Missing"
}else if(typeof copy.hazmat !== "boolean"){
errors.hazmat = "Invalid";
};
return errors;
};
const processObj = {
containerId: 55,
destination: "Carmel",
weight: 400,
unit: "lb",
hazmat: false }
function processManifest(manifest){
const valid = validateManifest(manifest);
if(Object.keys(valid).length > 0){
console.log(`Validation error: ${manifest.containerId}`);
console.log(valid);
}else {
const normal = normalizeUnits(manifest);
console.log(`Validation success: ${normal.containerId}`);
console.log(`Total weight: ${normal.weight} ${normal.unit}`);
}
};
processManifest({ containerId: -88, destination: "Soledad", weight: NaN });