Tell us what’s happening:
Everything works except for step 20. The answer seems to be correct. I’ve tried rearranging and changing things.
Your code so far
const blech = {
containerId: 68, potato: "glue", spanish: "glue", weight: 101, unit: "lb", hazmat: true
};
function normalizeUnits(manifest) {
const newManifest = {...manifest};
if (manifest.unit = "lb") {
newManifest.weight = manifest.weight * 0.45;
newManifest.unit = "kg"
return newManifest;
} else {
return newManifest;
}
}
function validateManifest(manifest) {
let newManifest = {...manifest};
let arr = ["containerId", "destination", "weight", "unit", "hazmat"];
let manArr = [];
for (const item in newManifest) {
manArr.push(item);
}
for (const thing of arr) {
if (!manArr.includes(thing)) {
newManifest[thing] = "Missing";
} else if (manArr.includes(thing) && thing === "containerId" && newManifest[thing] > 0 && Number.isInteger(newManifest[thing]) || thing === "destination" && typeof newManifest[thing] === "string" && newManifest[thing].trim() !== "" || thing === "weight" && newManifest[thing] > 0 || thing === "unit" && newManifest[thing] === "lb" || newManifest[thing] === "kg" || thing === "hazmat" && newManifest[thing] === true || newManifest[thing] === false) {
delete newManifest[thing];
} else {
newManifest[thing] = "Invalid"
}
}
for (const el of manArr) {
if (!arr.includes(el)) {
newManifest[el] = "Invalid";
}
}
return newManifest;
}
function processManifest(manifest) {
let newManifest = {...validateManifest(manifest)};
for (const prop in newManifest) {
if (newManifest[prop] === "Missing" || newManifest[prop] === "Invalid") {
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest)); return "";
}
}
console.log(`Validation success: ${manifest.containerId}`);
newManifest = normalizeUnits(manifest)
console.log(`Total weight: ${newManifest.weight} kg`); return "";
}
console.log(processManifest({ containerId: 55, destination: "Carmel", weight: 400, unit: "lb", hazmat: false }));
console.log(processManifest({}));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator