Tell us what’s happening:
Why are these testcases failing? Please help me to understand the problem
// running tests
4. Your normalizeUnits function should return a copy of the input manifest object with its weight normalized to kilograms and its unit set to “kg”. Use the approximate conversion 1 lb = 0.45 kg for the weight conversion.
16. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.
// tests completed
Your code so far
var normalizeUnits = (manifest) => {
return {
...manifest,
weight : manifest.weight * 0.45,
unit : 'kg'
};
}
console.log(normalizeUnits({ containerId: 68, destination: "Salinas", weight: 101, unit: "lb", hazmat: true }))
var validateManifest = (manifest) => {
const errors = {};
if (!("containerId" in manifest)) {
errors.containerId = "Missing";
}
else if (
!Number.isInteger(manifest.containerId) ||
manifest.containerId <= 0
) {
errors.containerId = "Invalid";
}
if (!("destination" in manifest)) {
errors.destination = "Missing";
}
else if (
typeof manifest.destination !== "string" ||
manifest.destination.trim() === ""
) {
errors.destination = "Invalid";
}
if (!("weight" in manifest)) {
errors.weight = "Missing";
}
else if (
typeof manifest.weight !== "number" ||
manifest.weight < 0 ||
Number.isNaN(manifest.weight)
) {
errors.weight = "Invalid";
}
if (!("unit" in manifest)) {
errors.unit = "Missing";
}
else if (
manifest.unit !== "kg" &&
manifest.unit !== "lb"
) {
errors.unit = "Invalid";
}
if (!("hazmat" in manifest)) {
errors.hazmat = "Missing";
}
else if (
typeof manifest.hazmat !== "boolean"
) {
errors.hazmat = "Invalid";
}
return errors;
};
var processManifest = (manifest) => {
const errors = validateManifest(manifest);
if (Object.keys(errors).length === 0) {
if(manifest.unit !== "kg") {
manifest = normalizeUnits(manifest);
}
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${manifest.weight} kg`);
}
else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(errors);
}
};
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