Tell us what’s happening:
Hi there,
I have troubles with passing test 15, and I don’t know why.
Originally I thought it was because of using “in” instead of “hasOwnProperty”, but it’s not the case.
Also, when i log typeof validateManifest(manifest), it logs object → the validateManifest function returns object.
What am I doing wrong here?
Your code so far
const normalizeUnits = manifest => {
const newObj = {...manifest};
if (newObj.unit == "lb") {
newObj.unit = "kg";
newObj.weight = newObj.weight * 0.45;
}
return newObj
}
const validateManifest = manifest => {
const newObj = {};
if (!(manifest.hasOwnProperty("containerId"))) {
newObj['containerId'] = "Missing"
} else if (!Number.isInteger(manifest.containerId) || manifest.containerId <= 0) {
newObj['containerId'] = "Invalid"
}
if (!(manifest.hasOwnProperty("destination"))) {
newObj['destination'] = "Missing"
} else if (typeof manifest.destination != "string" || !(manifest.destination.trim())) {
newObj['destination'] = "Invalid"
}
if (!(manifest.hasOwnProperty("weight"))) {
newObj['weight'] = "Missing";
} else if (manifest["weight"] < 0 || typeof manifest.weight != "number" || isNaN(manifest.weight)) {
newObj['weight'] = "Invalid";
}
if (!(manifest.hasOwnProperty("unit"))) {
newObj['unit'] = "Missing";
} else if (!(manifest.unit == "kg" || manifest.unit =="lb")) {
newObj['unit'] = "Invalid";
}
if (!(manifest.hasOwnProperty("hazmat"))) {
newObj['hazmat'] = "Missing";
} else if ((typeof manifest.hazmat) != "boolean") {
newObj['hazmat'] = "Invalid";
}
return newObj
}
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))
}
}
/*
const manifest = {containerId: "3", destination: 34, hazmat: false}
console.log(validateManifest(manifest))
console.log(typeof validateManifest(manifest))
*/
const manifest = {};
validateManifest(manifest);
console.log(typeof validateManifest(manifest)) //object
console.log(manifest) // {} - doesn't change original manifest
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:142.0) Gecko/20100101 Firefox/142.0
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator