Tell us what’s happening:
Stuck on test #15. Can’t seem to find the problem because the test result doesn’t provide more info regarding the error. It just says that my function should return an object with either the missing or invalid descriptions, which I believe it does. My code looks like a mess compared to cleaner versions I’ve seen here, so sorry about that.
Your code so far
const manifest = {
containerId: 46,
destination: "something",
weight: 40,
unit: "kg",
hazmat: false
}
// ----------- NORMALIZE UNITS FUNCTION
const normalizeUnits = (manifest) => {
let normalizedUnits = {...manifest};
if (manifest.unit == "lb") {
normalizedUnits.unit = "kg";
normalizedUnits.weight = (manifest.weight * 0.45);
}
return normalizedUnits;
}
// ----------- VALIDATE MANIFEST FUNCTION
const validateManifest = (manifest) => {
const validatedManifest = {...manifest};
// check for invalid or missing value in "containerId" property
if (typeof(manifest.containerId) == "string" || manifest.containerId <= 0) {
validatedManifest.containerId = "Invalid";
} else if (manifest.containerId == "" || manifest.containerId == undefined) {
validatedManifest.containerId = "Missing";
} else if (Number.isInteger(manifest.containerId) === false) {
validatedManifest.containerId = "Invalid";
} else {
delete validatedManifest.containerId;
}
// check for invalid or missing value in "destination" property
if (manifest.destination == "" || manifest.destination == undefined) {
validatedManifest.destination = "Missing";
} else if (typeof(manifest.destination) !== "string" || manifest.destination.trim() == "") {
validatedManifest.destination = "Invalid";
} else {
delete validatedManifest.destination;
}
// check for invalid or missing value in "weight" property
if (typeof(manifest.weight) == "string" || manifest.weight <= 0 || Number.isNaN(manifest.weight)) {
validatedManifest.weight = "Invalid";
} else if (manifest.weight == "" || manifest.weight == undefined) {
validatedManifest.weight = "Missing";
} else {
delete validatedManifest.weight;
}
// check for invalid or missing value in "unit" property
if (manifest.unit == "" || manifest.unit == undefined) {
validatedManifest.unit = "Missing";
} else if (typeof(manifest.unit) == "number") {
validatedManifest.unit = "Invalid";
} else if (manifest.unit.toLowerCase() == "kg") {
delete validatedManifest.unit;
} else if (manifest.unit.toLowerCase() == "lb") {
delete validatedManifest.unit;
} else {
validatedManifest.unit = "Invalid";
}
// check for invalid or missing value in "hazmat" property
if (manifest.hazmat === false) {
delete validatedManifest.hazmat;
} else if (manifest.hazmat === true) {
delete validatedManifest.hazmat;
} else if (manifest.hazmat == "" || manifest.hazmat == undefined) {
validatedManifest.hazmat = "Missing";
} else {
validatedManifest.hazmat = "Invalid";
}
return validatedManifest;
}
// ----------- PROCESS MANIFEST FUNCTION
const processManifest = (manifest) => {
if (Object.keys(validateManifest(manifest)).length == 0) {
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));
}
}
// ------------ TESTS
// console.log(normalizeUnits(manifest));
// console.log(validateManifest(manifest));
// processManifest(manifest);
// console.log(validateManifest({}));
// console.log(validateManifest({ destination: " ",}))
// console.log(validateManifest({ weight: NaN }));
console.log(validateManifest(manifest));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:150.0) Gecko/20100101 Firefox/150.0
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator