Tell us what’s happening:
What wrong with my code, I cannot pass story number 21, 25 and 26
Your code so far
const cargo1 = {
containerId: 1,
destination: "Monterey, California, USA",
weight: 8,
unit: "kg",
hazmat: false
};
const cargo2 = {
containerId: 2,
destination: "Chicago, USA",
weight: 300,
unit: "lb",
hazmat: true
};
const cargo3 = {
containerId: 3,
destination: "",
weight: "heavy",
unit: "lb",
hazmat: true
};
const normalizeUnits = function(manifest) {
if (manifest.unit === "lb"){
return {
...manifest,
weight: manifest.weight * 0.45,
unit: "kg"
};
}
return {...manifest};
}
const validateManifest = function(manifest) {
const missing = {}
if (!manifest.hasOwnProperty("containerId")){
missing.containerId = "Missing"
}else if (typeof(manifest.containerId) !== "number" || manifest.containerId <= 0 || !Number.isInteger(manifest.containerId)){
missing.containerId = "Invalid"
}
if (!manifest.hasOwnProperty("destination")){
missing.destination = "Missing"
}else if (typeof(manifest.destination) !== "string" || manifest.destination.trim().length === 0){
missing.destination = "Invalid"
}
if (!manifest.hasOwnProperty("weight")){
missing.weight = "Missing"
}else if (typeof(manifest.weight) !== "number" || manifest.weight <= 0 || Number.isNaN(manifest.weight)){
missing.weight = "Invalid"
}
if (!manifest.hasOwnProperty("unit")){
missing.unit = "Missing"
}else if (typeof(manifest.unit) !== "string" || manifest.unit.trim().length === 0){
missing.unit = "Invalid"
}else if (manifest.unit !== "kg" && manifest.unit !== "lb"){
missing.unit = "Invalid"
}
if (!manifest.hasOwnProperty("hazmat")){
missing.hazmat = "Missing"
}else if (typeof(manifest.hazmat) !== "boolean"){
missing.hazmat = "Invalid"
}
return {...missing}
}
const processManifest = function(manifest) {
let newManifest= validateManifest(manifest)
if(newManifest.containerId === "Invalid" || newManifest.containerId === "Missing"){
console.log(`Validation error: ${manifest.containerId}`)
console.log(newManifest);
}else {
console.log(`Validation success: ${normalizeUnits(manifest)}`)
console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`)
}
}
console.log(processManifest({ containerId: -88, destination: "Soledad", weight: NaN }))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36 Edg/149.0.0.0
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator