Tell us what’s happening:
Can someone help me? i cant pass these two goals: 13: Calling validateManifest() with { destination: " " } should return the new object { containerId: “Missing”, destination: “Invalid”, weight: “Missing”, unit: “Missing”, hazmat: “Missing” } without mutating the source input. You can use String.trim() to remove whitespace from a string.
If the input manifest object is not valid, 15: your validateManifest function should return an object describing missing and/or invalid properties.
Your code so far
let cargo1 = {
containerId: 101,
destination: "New York",
weight: 100,
unit: "lb",
hazmat: false,
};
const normalizeUnits = (manifest) => {
const newManifest = { ...manifest };
if (newManifest.unit.toLowerCase() === "lb") {
newManifest.weight = Number((newManifest.weight * 0.45).toFixed(2));
newManifest.unit = "kg";
}
return newManifest;
};
const validateManifest = (manifest) => {
const errors = {};
if (!Object.hasOwn(manifest, "containerId")) {
errors.containerId = "Missing";
} else if (
typeof manifest.containerId !== "number" ||
!Number.isInteger(manifest.containerId) ||
manifest.containerId <= 0
) {
errors.containerId = "Invalid";
}
if (!Object.hasOwn(manifest, "destination")) {
errors.destination = "Missing";
} else if (
typeof manifest.destination !== "string" ||
manifest.destination === ""
) {
errors.destination = "Invalid";
}
if (!Object.hasOwn(manifest, "weight")) {
errors.weight = "Missing";
} else if (
typeof manifest.weight !== "number" ||
Number.isNaN(manifest.weight) ||
manifest.weight <= 0
) {
errors.weight = "Invalid";
}
if (!Object.hasOwn(manifest, "unit")) {
errors.unit = "Missing";
} else if (manifest.unit !== "kg" && manifest.unit !== "lb") {
errors.unit = "Invalid";
}
if (!Object.hasOwn(manifest, "hazmat")) {
errors.hazmat = "Missing";
} else if (typeof manifest.hazmat !== "boolean") {
errors.hazmat = "Invalid";
}
return errors;
};
const processManifest = (manifest) => {
const validationResult = validateManifest(manifest);
if (Object.keys(validationResult).length === 0) {
console.log(`Validation success: ${manifest.containerId}`);
const normalizedManifest = normalizeUnits(manifest);
console.log(`Total weight: ${normalizedManifest.weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
}
};
processManifest(cargo1)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator