Tell us what’s happening:
Hello, I am unable to pass test 9 (which i think consequently means i can’t pass 15) - all else are passing, I’m not sure where i’m going wrong!
Your code so far
//a function which takes manifest as a paramater//must not mutate orignial object
//create a copy of the original object to change instead
const normalizeUnits = (manifest) => {
let cloneManifest = Object.assign({}, manifest);
if (cloneManifest.unit === "lb"){
cloneManifest.unit ="kg";
cloneManifest.weight = manifest.weight * 0.45;}
return cloneManifest
}
// if the unit is in lb, change to kg AND *0.45
//else unchanged
//
const validateManifest = (manifest) => {
let cloneManifest = Object.assign({}, manifest);
//containerID
if(cloneManifest.containerId == null ){
cloneManifest.containerId = "Missing"
}
else if(!Number.isInteger(manifest.containerId) || manifest.containerId <= 0)
{cloneManifest.containerId = "Invalid"}
else{
delete cloneManifest.containerId;
}
//destination
if(cloneManifest.destination == null ){
cloneManifest.destination ="Missing";
}
else if (typeof cloneManifest.destination !== "string" || (cloneManifest.destination.trim().length === 0)) {
cloneManifest.destination = "Invalid" }
else{
delete cloneManifest.destination}
//weight
if(cloneManifest.weight == null ){
cloneManifest.weight = "Missing";
}
else if (isNaN(cloneManifest.weight) ||cloneManifest.weight < 0){
cloneManifest.weight = "Invalid"
}
else{
delete cloneManifest.weight
}
//unit
if (cloneManifest.unit == null){cloneManifest.unit = "Missing"}
else if (cloneManifest.unit !== "kg" && cloneManifest.unit!== "lb"){
cloneManifest.unit = "Invalid"
}
else{delete cloneManifest.unit}
//hazmat
if (cloneManifest.hazmat == null ){
cloneManifest.hazmat = "Missing";
}
else if(typeof cloneManifest.hazmat !== "boolean"){
cloneManifest.hazmat = "Invalid"
}
else {delete cloneManifest.hazmat};
return cloneManifest;
}
//process manifest
const processManifest = (manifest) =>{
const normalizedManifest = normalizeUnits(manifest);
//if the manifest object is valid "Validation success: ${containerId}"
if (Object.keys(validateManifest(manifest)).length ==0) {
console.log(`Validation success: ${manifest.containerId}`);
//then the manifest's weight in kg Total weight: ${weight} kg. Use normalizeUnits()
console.log(`Total weight: ${normalizedManifest.weight} kg`)
}
//if the manifest object is not validValidation error: ${containerId}
//then the object returned
else{
const validatedManifest = validateManifest(manifest);
console.log(`Validation error: ${manifest.containerId}`);
console.log(validatedManifest);
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Challenge Information:
Build a Cargo Manifest Validator - Build a Cargo Manifest Validator