Tell us what’s happening:
My code does what is required but I can’t seem to get test 16 to succeed?
I am a newbie to this whole coding thing. I fully understand the problem lies with my over complicated code… I just don’t know how to fix.
Your code so far
function normalizeUnits(manifest) {
if (manifest.unit === "lb") {
let newManifest = {...manifest};
newManifest.unit = "kg";
newManifest.weight = newManifest.weight * 0.45;
return newManifest;}
else {
let newManifest = {...manifest};
return newManifest;
}
}
function validateManifest(manifest) {
let invalidManifest = {};
if (manifest.containerId > 0 && typeof manifest.destination === "string" && manifest.weight > 0 && manifest.unit === "kg" || manifest.unit === "lb" && typeof manifest.hazmat === "boolean") {
let validManifest = {};
return validManifest;
}
else {
if (!manifest.containerId) {
invalidManifest.containerId = "Missing";
}
if (manifest.containerId <= 0 || manifest.containerId === null || manifest.containerId && manifest.containerId != Number.isInteger()) {
invalidManifest.containerId = "Invalid";
}
if (!manifest.destination) {
invalidManifest.destination = "Missing";
}
if (manifest.destination && typeof manifest.destination !== "string" || manifest.destination && manifest.destination.trim() === "") {
invalidManifest.destination = "Invalid";
}
if (!manifest.weight) {
invalidManifest.weight = "Missing";
}
if (manifest.weight <= 0 || Number.isNaN(manifest.weight)) {
invalidManifest.weight = "Invalid";
}
if (manifest.unit === null || !manifest.unit) {
invalidManifest.unit = "Missing";
}
if (typeof manifest.unit === "number" || typeof manifest.unit === "string" && manifest.unit != "kg" && manifest.unit != "lb") {
invalidManifest.unit = "Invalid";
}
if (manifest.hazmat === null || manifest.hazmat === undefined) {
invalidManifest.hazmat = "Missing";
}
if (manifest.hazmat && typeof manifest.hazmat !== "boolean") {
invalidManifest.hazmat = "Invalid";
}
return invalidManifest
}
}
function processManifest(manifest) {
if (manifest.containerId > 0 && typeof manifest.destination === "string" && manifest.weight > 0 && manifest.unit === "kg" || manifest.unit === "lb" && typeof manifest.hazmat === "boolean") {
let newManifest = normalizeUnits(manifest);
let containerId = newManifest.containerId;
let weight = newManifest.weight;
console.log(`Validation success: ${containerId}`);
console.log(`Total weight: ${weight} kg`);
}
else {
let containerId = manifest.containerId;
console.log(`Validation error: ${containerId}`);
console.log(validateManifest(manifest));
}
}
let manifest = {
containerId: 55,
destination: "Santa Cruz",
weight: 304,
unit: "kg",
hazmat: false
}
console.log(normalizeUnits(manifest));
console.log(validateManifest(manifest));
console.log(processManifest(manifest));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator