Tell us what’s happening:
All tasks has been ticked except (15. If the input manifest object is not valid, your validateManifest function should return an object describing missing and/or invalid properties.).
I think others having same issue, I thought my mistake might be different.
Your code so far
const normalizeUnits = (manifest) => {
const newManifest = {...manifest};
const object = {
containerId: newManifest.containerId,
destination: newManifest.destination,
weight: newManifest.weight,
unit: newManifest.unit,
hazmat: newManifest.hazmat
};
if(newManifest.unit === "lb"){
object.weight = Number(parseFloat(object.weight * 0.45));
object.unit = "kg";
} else {
object.weight = manifest.weight;
object.unit = manifest.unit;
};
return object;
};
console.log(normalizeUnits({ containerId: 68, destination: "Salinas", weight: 101, unit: "lb", hazmat: true }))
const validateManifest = (manifest) => {
const newMan = {};
if(!manifest.hasOwnProperty("containerId")){
newMan.containerId = "Missing";
}
else if(typeof manifest.containerId !== "number" || !Number.isInteger(manifest.containerId) || manifest.containerId <= 0){
newMan.containerId = "Invalid";
}
if(!manifest.hasOwnProperty("destination")){
newMan.destination = "Missing";
}
else if(typeof manifest.destination !== "string" ||
manifest.destination.trim() === ""){
newMan.destination = "Invalid";
}
if(!manifest.hasOwnProperty("weight")){
newMan.weight = "Missing";
}
else if(typeof manifest.weight !== "number" ||
Number.isNaN(manifest.weight) ||
manifest.weight <= 0){
newMan.weight = "Invalid";
}
if(!manifest.hasOwnProperty("unit")){
newMan.unit = "Missing";
}
else if(typeof manifest.unit !== "string" ||
!["kg", "lb"].includes(manifest.unit.toLowerCase())){
newMan.unit = "Invalid";
}
if(!manifest.hasOwnProperty("hazmat")){
newMan.hazmat = "Missing";
}
else if(typeof manifest.hazmat !== "boolean"){
newMan.hazmat = "Invalid";
}
return newMan;
};
console.log(validateManifest({ containerId: 1, destination: "Santa Cruz", weight: 304, unit: "kg", hazmat: false }));
const processManifest = (manifest) => {
const valMan = validateManifest(manifest);
const normalizedManifest = normalizeUnits(manifest);
if(Object.keys(valMan).length === 0){
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${normalizedManifest.weight} kg`);
} else {
console.log(`Validation error: ${manifest.containerId}`);
console.log(valMan);
};
};
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator