Tell us what’s happening:
Why am I passing everything except step 14? Is there something wrong or missing from my code?
Your code so far
let valid;
function normalizeUnits(manifest) {
let obj = {};
let newObj = Object.assign(obj, manifest);
if (manifest.unit == "lb") {
newObj.weight *= 0.45;
newObj.unit = "kg";
}
return newObj;
}
function validateManifest(manifest) {
let obj = {};
if (Object.hasOwn(manifest, "containerId") &&
manifest.containerId > 0 &&
Number.isInteger(manifest.containerId) &&
Object.hasOwn(manifest, "destination") &&
Object.hasOwn(manifest, "weight") &&
manifest.weight > 0 &&
!Number.isNaN(manifest.weight) &&
Object.hasOwn(manifest, "unit") &&
Object.hasOwn(manifest, "hazmat") &&
typeof manifest.containerId == "number" &&
typeof manifest.destination.trim() == "string" &&
manifest.destination !== "" &&
typeof manifest.weight == "number" ||
manifest.unit == "kg" ||
manifest.unit == "lb" &&
typeof manifest.hazmat == "boolean") {
valid = true;
return obj;
} else {
if (!Object.hasOwn(manifest, "containerId")) {
obj.containerId = "Missing";
} else if (typeof manifest.containerId !== "number" ||
manifest.containerId <= 0 ||
!Number.isInteger(manifest.containerId)) {
obj.containerId = "Invalid";
}
if (!Object.hasOwn(manifest, "destination")) {
obj.destination = "Missing";
} else if (typeof manifest.destination !== "string" ||
manifest.destination.trim() == "") {
obj.destination = "Invalid";
}
if (!Object.hasOwn(manifest, "weight")) {
obj.weight = "Missing";
} else if (typeof manifest.weight !== "number" ||
manifest.weight <= 0 ||
Number.isNaN(manifest.weight)) {
obj.weight = "Invalid";
}
if (!Object.hasOwn(manifest, "unit")) {
obj.unit = "Missing";
} else if (manifest.unit !== "kg" || manifest.unit !== "lb") {
obj.unit = "Invalid";
}
if (!Object.hasOwn(manifest, "hazmat")) {
obj.hazmat = "Missing";
} else if (typeof manifest.hazmat !== "boolean") {
obj.hazmat = "Invalid";
}
valid = false;
return obj;
}
}
function processManifest(manifest) {
validateManifest(manifest);
if (valid == true) {
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));
}
}
console.log(validateManifest({ containerId: 68, destination: "Salinas", weight: -101, unit: "lb", hazmat: true }));
processManifest({ containerId: -68, destination: "Salinas", weight: 101, unit: "lb", hazmat: true });
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