Build a Cargo Manifest Validator - Build a Cargo Manifest Validator

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

Github Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-cargo-manifest-validator/69a56b5069ca99f7317e6e19.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @841848!

Yes, it’s normal to feel out-of-touch with code when you’ve been away from it for a long time. Happens to all of us.

This code could be used to check if the containerId is missing, but not if it’s invalid.

Was there anything in the instructions about a minimum length for the destination string?

What did the instructions say you should validate? Was it whether containerId is missing or not? Please have another look at User Story #3.

These are just a few places to look to get you started. I have not tested your code through yet.

Happy coding!

Edit:

Try testing with this:
console.log(validateManifest({ containerId: 0, destination: 405, weight: -84, unit: "pounds", hazmat: 1 }))

Thank you very much for the warm welcome and your help.

I fixed my code from your instructions on these parts:
manifest.containerId === undefined
else if (manifest.destination.trim().length < 3) {
Also, the minimum length wasn’t needed.

I tried the testing code that you sent. And I saw my mistake there:
} else if (manifest.hazmat != true && manifest.hazmat != false) {
I should have used !== over there instead of !=. And now I just passed the lab.

But I think I should fix that part for the better: if(validateManifest(manifest).hasOwnProperty("containerId")) {

Thank you for your help and kindness!