Tell us what’s happening:
I am on the Build a Cargo Manifest Validator. My tests are matching the results in the Instructions, but I keep getting the error ,“If the input manifest object is valid, your processManifest function should log a success message with the object’s containerId, and then log the object’s weight in kilograms. You should use normalizeUnits() for the conversion and have two console.log() calls.” I have tried several different ways getting the correct output, but still not passing.
Your code so far
const normalizeUnits = (manifest) => {
const normalManifest = { ...manifest};
if (normalManifest.unit === "lb") {
normalManifest.weight *= 0.45;
normalManifest.unit = "kg";
}
return normalManifest;
};
const validateManifest = (manifest) => {
const validManifest = { ...manifest};
if (!Object.hasOwn(validManifest, "containerId")) {
validManifest.containerId = "Missing";
} else if (!Number.isInteger(validManifest.containerId) ||
validManifest.containerId <= 0) {
validManifest.containerId = "Invalid";
} else {
delete validManifest.containerId;
}
if (!Object.hasOwn(validManifest, "destination")) {
validManifest.destination = "Missing";
} else if (String(validManifest.destination).trim() === "" || typeof(validManifest.destination) != "string" ) {
validManifest.destination = "Invalid";
} else {
delete validManifest.destination;
}
if (!Object.hasOwn(validManifest, "weight")) {
validManifest.weight = "Missing";
} else if (validManifest.weight <= 0 || typeof(validManifest.weight) !== "number" || Number.isNaN(validManifest.weight)) {
validManifest.weight = "Invalid";
} else {
delete validManifest.weight;
}
if (!Object.hasOwn(validManifest, "unit")) {
validManifest.unit = "Missing";
} else if (validManifest.unit !== "kg" && validManifest.unit !== "lb") {
validManifest.unit = "Invalid";
} else {
delete validManifest.unit;
}
if (!Object.hasOwn(validManifest, "hazmat")) {
validManifest.hazmat = "Missing";
} else if (typeof(validManifest.hazmat) !== "boolean") {
validManifest.hazmat = "Invalid";
} else {
delete validManifest.hazmat;
}
return validManifest;
};
const processManifest = (manifest) => {
//const validManifest = {...validateManifest(manifest)}
if (Object.keys(validateManifest(manifest)).length === 0) {
//const normalManifest = {...normalizeUnits(manifest)};
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
};
processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false });
processManifest({ containerId: -88, destination: "Soledad", weight: NaN });
processManifest({ destination: "Watsonville", hazmat: true });
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator