Tell us what’s happening:
Hey there, my code doesn’t work for #15.
This lab was too hard for me. I couldn’t study JavaScript for a month, maybe that caused this. I usually solved previous labs on my own, but this one was too hard for me. I feel bad about it. I had to look for a little help with my code. Without the help, I probably wouldn’t have come this far in this lab. I wanted to write my thoughts. Is it normal? Besides the help of the lab code, any advice or thoughts will be great.
Thanks.
Your code so far
function normalizeUnits(manifest){
const newObj = {...manifest};
if (newObj.unit === "lb"){
newObj.weight *= 0.45;
newObj.unit = "kg";
return newObj;
} else {
return newObj;
}
}
function validateManifest(manifest) {
const errors = {};
if (!manifest.hasOwnProperty("containerId")) {
errors.containerId = "Missing";
} else if(manifest.containerId === null ||
manifest.containerId === undefined ||
manifest.containerId < 1 ||
!Number.isInteger(manifest.containerId) ) {
errors.containerId = "Invalid";
}
if (!manifest.hasOwnProperty("destination")) {
errors.destination = "Missing";
} else if (typeof manifest.destination !== "string") {
errors.destination = "Invalid";
} else if (manifest.destination.trim().length < 3) {
errors.destination = "Invalid";
}
if(!manifest.hasOwnProperty("weight")) {
errors.weight = "Missing";
} else if (Number.isNaN(manifest.weight)){
errors.weight = "Invalid";
} else if (manifest.weight < 1) {
errors.weight = "Invalid";
}
if (!manifest.hasOwnProperty("unit")) {
errors.unit = "Missing";
} else if(manifest.unit !== "kg" && manifest.unit !== "lb") {
errors.unit = "Invalid";
}
if (!manifest.hasOwnProperty("hazmat")) {
errors.hazmat = "Missing";
} else if (manifest.hazmat != true && manifest.hazmat != false) {
errors.hazmat = "Invalid";
}
return errors;
}
function processManifest(manifest) {
if(validateManifest(manifest).hasOwnProperty("containerId")) {
const weight = normalizeUnits(manifest.weight);
console.log(`Validation error: ${manifest.containerId}`);
console.log(validateManifest(manifest));
} else if (validateManifest(manifest)) {
console.log(`Validation success: ${manifest.containerId}`);
console.log(`Total weight: ${normalizeUnits(manifest).weight} kg`);
}
}
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