Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Tell us what’s happening:

El codigo supero el 99 por ciento de las pruebas, excepto la numero 15 .. no logro superarla, investigue pregunte y aun asi no pude lograr pasar la prueba .
si alguien pudo pasar la prueba o entendio el problema seria de gran ayuda enterder por que funciona y por que no funciono antes! gracias

Your code so far

function normalizeUnits(manifest){
    const copiManifest = {...manifest};
    if(manifest.unit === "lb"){
    const kilogramos = manifest.weight * 0.45;
    copiManifest.weight = kilogramos;
    copiManifest.unit = "kg";
    };
    return copiManifest;
};

function validateManifest(manifest){
    const errors = {};
    const tipes = ["containerId", "destination", "weight", "unit", "hazmat"];
    tipes.forEach((value)=>{
    
       

        if(!(value in manifest) || manifest[value] === undefined){
            errors[value] = "Missing";
            return
        }
         const values = manifest[value]

        switch(value){
            case "containerId":
                if(!Number.isInteger(values) || values <= 0 ) errors[value] = "Invalid";
                break;
            case "destination":
                if(typeof values !== "string" || values.trim().length === 0) errors[value] = "Invalid";
                break;
            case "weight":
                if(typeof values !== "number"  || Number.isNaN(values) || values < 0) errors[value] = "Invalid";
                break;
            case "unit":
                if(values !== "kg" && values !== "lb") errors[value] = "Invalid";
                break;
            case "hazmat":
                if(typeof values !== "boolean") errors[value] = "Invalid"
                break;
        }   
    });
    return errors;
};

function processManifest(manifest){
    const errors = validateManifest(manifest);

    if(Object.keys(errors).length === 0) {
        console.log(`Validation success: ${manifest.containerId}`)
        const normalized = normalizeUnits(manifest);
        console.log(`Total weight: ${normalized.weight} kg`)
    }else{
        console.log(`Validation error: ${manifest.containerId}`)
        console.log(errors);
    };
        
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

Welcome to the forum @maximontoya7!

Please debug your code with print(). For example, what is this code checking?

manifest[value] === undefined

And what is values here?

Happy coding!

if(!(value in manifest) || manifest[value] === undefined){
errors[value] = “Missing”;
return
}
const values = manifest[value]

aqui compruebo que la propiedad no exista en el objeto manifest , o si existe dicha propiedad , que sea undefined , para asi guardarla en la variable errors.

luego en esta linea ! /
const values = manifest[value] , values

cuando hago tipes.forEach((value)=>{ …
value contiene los elementos de tipes .
manifest[value] accede al elemento del objeto manifest, la propiedad .
y se almacena en values ..
espero mi explicacion sea entendible ..

I was asking those questions to point to an area of your code that might be an issue since manifest[value] is the value of the property, not the property itself, but you are using it in an expression that is attempting to check for a missing property.

But let’s move on.

If you open your browser’s console after running the tests, you will see an assertion error:

actual: {containerId: 'Invalid', destination: 'Invalid', unit: 'Invalid'}
expected: {containerId: 'Invalid', destination: 'Invalid', weight: 'Invalid', unit: 'Invalid'}

How is the object returned from your code different than what is expected?

1 Like

para retornar un resultado esperado , tuve que cambiar las condiciones para la propiedad “weight”

case “weight”:
if(typeof values !== “number” || Number.isNaN(values) || values <= 0) errors[value] = “Invalid”;

La consigna decia, que el numero de entrada no debia ser positivo . si no me equivoco 0 (cero) no es positivo, entoces agregue a la comparacion un (=) para que quede de esta manera ‘ values <= 0 ‘
de esta manera no deberia habre problema en la salida, ya que entraria en el caso “invalid”

1 Like